How can I quickly assign an array to another array by value instead of by reference without using a LOOP?
Example:
I have a Sub that has an array parameter and I want to create a local array to manipulate the values of the local array WITHOUT modifying the values of the parameter array.
Log:
aArray:1 2 3 4 5 6 7 8 9 10
locArray:1 20 3 4 5 6 7 8 9 10
MyArray:1 20 3 4 5 6 7 8 9 10
As you can see, when I use
What is an easy way to assign aArray to locArray (by value and not by reference) without using a loop?
There has to be a simple way of doing it, but for the life of me I can't seem to remember how.
TIA
Example:
I have a Sub that has an array parameter and I want to create a local array to manipulate the values of the local array WITHOUT modifying the values of the parameter array.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private MyArray(10) As Int = Array As Int(1,2,3,4,5,6,7,8,9,10)
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
End Sub
Sub Activity_Resume
TestArray(MyArray)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ArrayToStr(aArray() As Int) As String
Private SB As StringBuilder
SB.Initialize
For i=0 To aArray.Length-1
SB.Append(aArray(i)).Append(" ")
Next
Return SB.ToString
End Sub
Sub TestArray(aArray() As Int)
Log("aArray:" & ArrayToStr(aArray))
Private locArray() As Int
locArray = aArray
locArray(1) = locArray(1) * 10
Log("locArray:"&ArrayToStr(locArray))
Log("MyArray:"&ArrayToStr(MyArray))
End Sub
Log:
aArray:1 2 3 4 5 6 7 8 9 10
locArray:1 20 3 4 5 6 7 8 9 10
MyArray:1 20 3 4 5 6 7 8 9 10
As you can see, when I use
locArray = aArray
then change locArray(1), the passed parameter MyArray(1) also changes to 20. What is an easy way to assign aArray to locArray (by value and not by reference) without using a loop?
There has to be a simple way of doing it, but for the life of me I can't seem to remember how.
TIA