test if a variable is an array

luke2012

Well-Known Member
Licensed User
Longtime User
In one language that I used when I want to test if a variable contains an array I used the keyword "isArray".

There is something similar in B4A ?
 

agraham

Expert
Licensed User
Longtime User
Because Basic4android/Java is strongly typed the type of the variable is determined at compile time so you already know if it is an array or not as you wrote the code.

EDIT: There is one exception to this that I can think of in that an Object type variable can hold an Object array. However you cannot do anything useful with it without assigning it back to a variable that the compiler knows is an array variable.

On the other hand a variable typed as a reference type, for example Object or View, can hold references to other types as long as they inherit from that variable type. In this case the "Is" keyword can be used to test the type of the reference in the variable.

From the help for Is
B4X:
For i = 0 To Activity.NumberOfViews - 1
  If Activity.GetView(i) Is Button Then
   Dim b As Button
   b = Activity.GetView(i)
   b.Color = Colors.Blue
  End If
Next
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As Andrew said, there is probably very little use for it, but I think this code will work. It did in those I tested it on, so test it on those types you want it to work on.

Requires the reflection Library:

B4X:
Sub IsArray(sArr As Object) As Boolean

   Dim R As Reflector
   Dim Test() As Int
   R.Target = sArr
   Test = R.TargetRank
   If Test.Length > 0 Then Return True
   Return False
End Sub
 
Upvote 0
Top