problem with adding item(define by type) to list

avnersh

Member
Licensed User
Longtime User
Hello all
I'm trying to use the list object by adding to the list an item than define by type
Actually once I added new item ,all the previous items in list become the
some as the new inserted item.
This is my simple code that demonstrate the problem
Am i using the list in the right way?
Thank
avner shmuel

Sub Process_Globals
Type equation_group(name As String,pictur_file As String)
Dim list1 As List
Dim group_test As equation_group
Dim i As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
group_test.Initialize
list1.Initialize

For i= 1 To 10
group_test.name="n"& i
group_test.pictur_file="p"& i
list1.Add(group_test)
Next
End Sub

 
Last edited:

Kamac

Active Member
Licensed User
Longtime User
For v= 1 To 10
group_test.name="n"& i
group_test.pictur_file="p"& i
list1.Add(group_test)
Next

Um...

That makes no sense, because you do:

For v = 1

And you try to include "i".


list1.Add(group_test)

Looks strange also. You could use:

list1.Add(group_test.name & " " & group_test.pictur_file)

I'm unsure if list1.Add(group_test) is right, i'd change it.
 
Upvote 0

avnersh

Member
Licensed User
Longtime User
Hi KAMAC
First of all thank you ...
The V instead of i in the for next loop is my typing mistake :signOops:
Yet I'm not understanding way the list cannot accept an structure item
in the manual the item is note define as a string but as an object
Add (item As Object)

Regards
Avner
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Indeed, as object, but imagine what could a type contain?
If you can't post something relevant to the problem please don't post at all and stop cluttering the forum.
all the previous items in list become the some as the new inserted item.
This is a very common mistake. You need to Dim a new type inside the loop to get a new instance otherwise you are using the same instance of your type all the time. Read about it here in the first section "Types".
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to Dim the variable group_test in the For / Next loop.
B4X:
[COLOR=black]For i= 1 To 10 [/COLOR]
[COLOR=black][COLOR=#ff0000]  Dim group_test As equation_group[/COLOR]
  group_test.name="n"& i
  group_test.pictur_file="p"& i
  list1.Add(group_test)
Next [/COLOR]
Otherwise, when adding the last entry, you change the variables but they are the same for each entry. Diming in the loop you have a new onr each iteration.

Best regards.
 
Upvote 0

avnersh

Member
Licensed User
Longtime User
agraham and klaus, thank you very much. you saved my first B4A application.
:sign0098:
Best regards
Avner shmuel
 
Upvote 0
Top