Add String Array to List

B4AJunkie

Member
Licensed User
Longtime User
Can someone please explain me how to add an Stringarray to a list?

Please look at the following code. Why is the list DataFault completly filled with the content of the last build Array StrArrFault and differs from Data.

Look at the Logfile:
3
Data(0,0): 1
DataFault(0,0): 3 <- should be 1
Data(0,1): 11
DataFault(0,1): 33 <- should be 11
Data(0,2): 111
DataFault(0,2): 333 <- should be 111
Data(1,0): 2
DataFault(1,0): 3 <- should be 2
Data(1,1): 22
DataFault(1,1): 33 <- should be 22
Data(1,2): 222
DataFault(1,2): 333 <- should be 222
Data(2,0): 3
DataFault(2,0): 3
Data(2,1): 33
DataFault(2,1): 33
Data(2,2): 333
DataFault(2,2): 333

What do i do wrong?

Here is the code:
B4X:
'Activity module
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.
Dim Data As List
Dim DataFault As List
End Sub

Sub Activity_Create(FirstTime As Boolean)

Dim StrArr() As String
Dim StrArrFault(3) As String
Data.Initialize
DataFault.Initialize
Log("1")
StrArr = Array As String ("1","11","111")
Data.Add(StrArr)
StrArrFault(0)="1"
StrArrFault(1)="11"
StrArrFault(2)="111"
DataFault.Add(StrArrFault)
LogData
Log("2")
StrArr = Array As String ("2","22","222")
Data.Add(StrArr)
StrArrFault(0)="2"
StrArrFault(1)="22"
StrArrFault(2)="222"
DataFault.Add(StrArrFault)
LogData
Log("3")
StrArr = Array As String ("3","33","333")
Data.Add(StrArr)
StrArrFault(0)="3"
StrArrFault(1)="33"
StrArrFault(2)="333"
DataFault.Add(StrArrFault)
LogData

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub LogData
Dim inh() As String
Dim inh2() As String
   For col=0 To Data.Size -1
      inh=Data.Get(col)
      inh2=DataFault.Get(col)
      For col2=0 To inh.Length -1
         Log("Data(" & col & "," & col2 & "): " & inh(col2))
         Log("DataFault(" & col & "," & col2 & "): " & inh2(col2))
      Next
   Next
End Sub

Andreas
 

klaus

Expert
Licensed User
Longtime User
You need to Dim new instances of the string arrays before filling them:
B4X:
Dim StrArr() As String
Dim StrArrFault(3) As String
Data.Initialize
DataFault.Initialize
Log("1")
StrArr = Array As String ("1","11","111")
Data.Add(StrArr)
StrArrFault(0)="1"
StrArrFault(1)="11"
StrArrFault(2)="111"
DataFault.Add(StrArrFault)
LogData
Log("2")
Dim StrArr() As String
StrArr = Array As String ("2","22","222")
Data.Add(StrArr)
Dim StrArrFault(3) As String
StrArrFault(0)="2"
StrArrFault(1)="22"
StrArrFault(2)="222"
DataFault.Add(StrArrFault)
LogData
Log("3")
Dim StrArr() As String
StrArr = Array As String ("3","33","333")
Data.Add(StrArr)
Dim StrArrFault(3) As String
StrArrFault(0)="3"
StrArrFault(1)="33"
StrArrFault(2)="333"
DataFault.Add(StrArrFault)
LogData
Best regards.
 
Upvote 0

B4AJunkie

Member
Licensed User
Longtime User
Thanks again Klaus.

It took me a whole day to break down this error in my project to the shown lines.

I still do not know why I have to redim the Arrays, because reading out the arrays work as expected without "dimming" the instances again.

But, now it is working. Thank you!
:sign0098:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to Dim the arrays because of the Lists.
You add an array to a List that means this List item is pointing to that array.
Then you change the values of the array and add the same array as a new item in the List. Both List items point to the same array so all List items pointing to the same array have same values the last ones.
Diming the arrays before changing the values initates a new array independant or the previous one event if the name is the same.
I hope the explanation is clear.

Best regards.
 
Upvote 0

B4AJunkie

Member
Licensed User
Longtime User
I did not know that pointers are being used.
I guess that is the same thing, I experienced with this code:
B4X:
Dim new(1) as String
Dim old(1) as String
new(0)="Old Value"
old=new
new(0)="New Value"
log(old(0))
The log will show New Value and not Old Value as I expected.

Thank you.
 
Upvote 0
Top