Android Question Array of INT pointers?

TheWind777

Active Member
Licensed User
Longtime User
Is it possible to have some Global Variables that have different names, and you create an array of those variable names so the actual original variables could be modified? (An array that would be a pointer to the actual storage area of that variable; so if you assigned a value to the array item, it would modify the Int.

Such as:

B4X:
Sub Process_Globals

  Dim ThisVariable as Int = 0
  Dim ThatVariable as Int = 0
  Dim TheOtherVariable as Int = 0

  Dim ArrayOfInts()
  ArrayOfInts = Array as Int(&ThisVariable,&ThatVariable,&TheOtherVariable)  ' Where the & means _ something that would make this mean a POINTER to ThisVariable, etc.)

End Sub

Sub Activity_Create(FirstTime AsBoolean)
  Dim z as Int

  for z = 0 to 2
     ArrayOfInts(z) = z
  next

  ' Here, I would like to have changed the globals so that ThisVariable = 1, ThatVariable = 2, TheOtherVariable = 3

end sub


Is there any way to truly pass the value to the global variable from the array (or from a list... or anything?)
 

PaulR

Active Member
Licensed User
Longtime User
Java/b4a doesn't allow passing primitive variables by reference so you can define a new Type and use that...
B4X:
Sub Process_Globals
     Type AnInt (x As Int)
     Dim thisVariable As AnInt : thisVariable.x=2
     Dim thatVariable As AnInt : thatVariable.x=1
     Dim theOtherVariable As AnInt : theOtherVariable.x=0
     Dim ArrayOfInts() As AnInt
     ArrayOfInts=Array As AnInt(thisVariable, thatVariable, theOtherVariable)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim z As Int
    For z = 0 To 2
        ArrayOfInts(z).x = z
    Next

    thisVariable.x=10 : thatVariable.x=20 : theOtherVariable.x=30

    For z = 0 To 2
        Log(ArrayOfInts(z).x)     
    Next
End Sub
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
Java/b4a doesn't allow passing primitive variables by reference so you can define a new Type and use that...
B4X:
Sub Process_Globals
     Type AnInt (x As Int)
     Dim thisVariable As AnInt : thisVariable.x=2
     Dim thatVariable As AnInt : thatVariable.x=1
     Dim theOtherVariable As AnInt : theOtherVariable.x=0
     Dim ArrayOfInts() As AnInt
     ArrayOfInts=Array As AnInt(thisVariable, thatVariable, theOtherVariable)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim z As Int
    For z = 0 To 2
        ArrayOfInts(z).x = z
    Next

    thisVariable.x=10 : thatVariable.x=20 : theOtherVariable.x=30

    For z = 0 To 2
        Log(ArrayOfInts(z).x)    
    Next
End Sub

That was extremely helpful!

Thanks.
 
Upvote 0
Top