checking if value exists in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BlackMustard
    New Member
    • Aug 2007
    • 88

    checking if value exists in array

    hello

    i am looking for a boolean function to be used like this:

    [code=vb]if InArray(val,arr ()) then
    ' some code to run if the value "val" exists in one of the posts of the array "arr"
    else
    ' some code to run if it doesn't
    end if[/code]

    i guess i could write my own function where i loop through the array and look for the value, and if found i return true - but then i'd have to write a new version for each type i want to use (i.e. one for file arrays, one for string arrays etc.) is there a more efficient way?
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Have you tried using ISNULL() .

    Comment

    • BlackMustard
      New Member
      • Aug 2007
      • 88

      #3
      Originally posted by debasisdas
      Have you tried using ISNULL() .
      isnull() would not give me an answer to my question. i clarify with an example:

      i have a string array, arrStrings(), containing five different strings. i have a variable, strValue, containing a value i want to test for. what i want is a function that, regardless of the data types (in this case string), can return to me whether or not strValue is exactly the same as any one or more of the elements in arrStrings().

      A function to do what i want for strings would be this:

      [code=vb]Function InStrArray(val as string, arr() as string)
      InStrArray = False

      dim i as integer
      for i = lbound(arr) to ubound(arr)
      if arr(i) = value then InStrArray = True
      next
      End Function[/code]

      I want to do the same, but regardless of datatypes. That is, I want to be able to use my function on string arrays as well as file, control and other arrays.

      Comment

      Working...