Android Question List Adding By Value Rather than Reference

DawningTruth

Active Member
Licensed User
I am running the code analogous to the following:

B4X:
    GroupOfElements As List

    For n = 0 To 4       
 
       Element.Initialize
       Element.ID = n
       Element.TextField = "Element " + n
              
       GroupOfElements.Add(Element)
              
   Next

The idea is to add a couple of elements to a list.

The code adds the elements as reference. This means that every element is identical and you get a list filled with:

GroupOfElements
[0] Element.ID = 4; Element.TextField = "Element 4"
[1] Element.ID = 4; Element.TextField = "Element 4"
[2] Element.ID = 4; Element.TextField = "Element 4"
[3] Element.ID = 4; Element.TextField = "Element 4"
[4] Element.ID = 4; Element.TextField = "Element 4"

Is there any way I can add the elements by value instead of reference?
 

Star-Dust

Expert
Licensed User
Longtime User
To avoid addressing the same variable does not enough initialize but you have to DIM it every time
B4X:
GroupOfElements As List

    For n = 0 To 4   
       Dim Element as MyTYpe
       Element.Initialize
       Element.ID = n
       Element.TextField = "Element " + n
          
       GroupOfElements.Add(Element)
          
   Next
 
Last edited:
Upvote 0

DawningTruth

Active Member
Licensed User
To avoid addressing the same variabiel does not enough initialize but you have to DIM it every time
B4X:
GroupOfElements As List

    For n = 0 To 4   
       Dim Element as MyTYpe
       Element.Initialize
       Element.ID = n
       Element.TextField = "Element " + n
          
       GroupOfElements.Add(Element)
          
   Next

Thx that sorted it :)
 
Upvote 0
Top