Android Code Snippet Checks on arrays

LucaMs

Expert
Licensed User
SubNames: IsArray, ArrayType.

Descriptions:
IsArray checks if an object variable is an array.
Return value: Boolean - True if the variable is an array.

ArrayType checks the type of data contained in an array.
Return value: String - Type of data ("String", "Int", "Float"...)
[Correction: it returned "String" also for arrays of objects]

IsArray
B4X:
Public Sub IsArray(Var As Object) As Boolean
    Dim VarType As String = GetType(var)
    Return VarType.StartsWith("[")
End Sub


ArrayType
B4X:
Public Sub ArrayType(Var As Object) As String
    Dim Res As String

    Dim VarType As String = GetType(Var)

    If VarType.StartsWith("[") Then

        Dim SecondChar As String = VarType.SubString2(1,2)
        Select Case SecondChar
            Case "B"
                Res = "Byte"
            Case "S"
                Res = "Short"
            Case "I"
                Res = "Int"
            Case "J"
                Res = "Long"
            Case "F"
                Res = "Float"
            Case "D"
                Res = "Double"
            Case "C"
                Res = "Char"
            Case "L"
                If VarType.Contains("String") Then
                    Res = "String"
                Else
                    Res = "Object"
                End If
            Case Else
                Res = ""
        End Select

    End If

    Return Res

End Sub
Link to the original post.


Example and test:
B4X:
Private Sub Test

    Private arrByte() As Byte
    Private arrShort() As Short
    Private arrInt() As Int
    Private arrLong() As Long
    Private arrFloat() As Float
    Private arrChar() As Char
    Private arrString() As String

    Log("arrByte is array = " & IsArray(arrByte))
    Log("arrShort is array = " & IsArray(arrShort))
    Log("arrInt is Array = " & IsArray(arrInt))
    Log("arrLong is array = " & IsArray(arrLong))
    Log("arrFloat is array = " & IsArray(arrFloat))
    Log("arrChar is array = " & IsArray(arrChar))
    Log("arrString is array = " & IsArray(arrString))
    Log("_________________________________")
    Log("Type of arrByte is " & ArrayType(arrByte))
    Log("Type of arrShort is " & ArrayType(arrShort))
    Log("Type of arrInt is " & ArrayType(arrInt))
    Log("Type of arrLong is " & ArrayType(arrLong))
    Log("Type of arrFloat is " & ArrayType(arrFloat))
    Log("Type of arrChar is " & ArrayType(arrChar))
    Log("Type of arrString is " & ArrayType(arrString))

End Sub
LOG:
arrByte is array = true
arrShort is array = true
arrInt is Array = true
arrLong is array = true
arrFloat is array = true
arrChar is array = true
arrString is array = true
_______________________
Type of arrByte is Byte
Type of arrShort is Short
Type of arrInt is Int
Type of arrLong is Long
Type of arrFloat is Float
Type of arrChar is Char
Type of arrString is String



Tags: Array, Types, GetType
 
Last edited:

raphaelcno

Active Member
Licensed User
You can add the condition "If Var <> Null" in order to prevent a crash when using IsArray(View.Tag) in case View.Tag is not defined:

B4X:
Public Sub IsArray(Var As Object) As Boolean
    ' New line
    If Var <> Null Then
        Dim VarType As String = GetType(Var)
        Return VarType.StartsWith("[")
    End If
End Sub

Public Sub ArrayType(Var As Object) As String
  
   ' New line
   If Var <> Null Then
      
     Dim Res As String
    
     Dim VarType As String = GetType(Var)
    
     If VarType.StartsWith("[") Then
    
       Dim SecondChar As String = VarType.SubString2(1,2)
       Select Case SecondChar
         Case "B"
           Res = "Byte"
         Case "S"
           Res = "Short"
         Case "I"
           Res = "Int"
         Case "J"
           Res = "Long"
         Case "F"
           Res = "Float"
         Case "D"
           Res = "Double"
         Case "C"
           Res = "Char"
         Case "L"
           Res = "String"
         Case Else
           Res = ""
       End Select
    
     ' New line
     Else
       Res = "Not Array"
      
     End If
     
   ' New line
   Else
      Res = "Not defined"
    
   End If
   
   Return Res
  
End Sub
 

LucaMs

Expert
Licensed User
Hi Raphael,

thanks for your advice.

I'm thinking about it.

If I add that check, the two functions would return False and an empty string respectively. The programmer would have no way of knowing that he has passed a null parameter. In fact, he should perform the check before calling the function, so it is right that the app crashes.
 
Top