List fo user defined 'types'

gmlogan

Member
Licensed User
Longtime User
Hi
I am trying to implement a list of user defined types. The documentation for the 'list' says it can hold any object, but I am finding that with a user defined type this is not working as I expected.

Basically I am trying to use a list object to hold named/pairs, I have defined a 'type' with a string and an int, I can then create these and access the properties/values via the . notation (and intellitype shows them).

I can add them to a list object with no errors, but when I try to retrieve then I only seem to get the last one no matter what position in the list I 'Get'.

Example is below, the first 3 rows of the list view get updated as expected, showing to me that the user defined type 'b' is working.

I would expect the last 3 rows to be repeats as I am getting from each position in the list, but I get the last item added from each position? I am getting an object of the correct type back into 'c' but it is always the same one?

Thoughts, or point out the obvious (but not to me:-() mistake

Graham
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ListView1 As ListView
   Type namedpair (name As String, val As Int)
End Sub

Sub Activity_Create(FirstTime As Boolean)
activity.LoadLayout("tab1layout")
Dim b As namedpair
Dim c As namedpair
Dim List1 As List

list1.Initialize
'action 1
b.name="Fred"
b.val=20
list1.Add(b)
listview1.AddSingleLine(b.name & "="& b.val)
'action 2
b.name="Bob"
b.val=30
list1.Add(b)
listview1.AddSingleLine(b.name & "="& b.val)
'action 3
b.name="Mary"
b.val=40
list1.Add(b)
listview1.AddSingleLine(b.name & "="& b.val)
'action 4
c=list1.Get(0)
listview1.AddSingleLine(c.name & "="& c.val)
'action 5
c=list1.Get(1)
listview1.AddSingleLine(c.name & "="& c.val)
'action 6
c=list1.Get(2)
listview1.AddSingleLine(c.name & "="& c.val)


End Sub
 

gmlogan

Member
Licensed User
Longtime User
screen shot
 

Attachments

  • screen1.jpg
    screen1.jpg
    8.2 KB · Views: 254
Upvote 0

agraham

Expert
Licensed User
Longtime User
I am getting an object of the correct type back into 'c' but it is always the same one?
That's because it is the same one!

Dim b As namedpair ' makes a namedpair instance and points b at it

Every time from now on you are using the same named pair and just changing it's values. You need another

Dim b As namedpair ' makes a different namedpair instance and points b at it

to break the reference that b holds to the first namedpair instance. Just like arrays.
 
Upvote 0

gmlogan

Member
Licensed User
Longtime User
Sorry I do not follow.
b is a variable of type namedpair, list1 is a list to which I am adding (to the end) the value of b, as I do this I am writing the value of b to the listview to form the 1st three rows and they all change.

Then I start reading back the items I stored in list1 based on the index position (0,1,2,), and they are all the same?

To me the first 3 tasks shows that the variable b (or type namepair) is getting updated, so I would expect the values to get added to the list1.

Or are you saying what is added to list1 is in fact a 'pointer' to b, thus I am adding 3 pointers to list1, and they all point to b, thus on reading back I get the last value set??

If so, how would I go about this, basically I want to read a DB in one sub hold the values of the query in some structure that I can pass around to other subs. I have all the DB stuff working etc
 
Upvote 0

gmlogan

Member
Licensed User
Longtime User
Thanks, got it, and that last line of dim b, each time works. I guess I could run into problems with memory with lots of b's being created,
 
Upvote 0

gmlogan

Member
Licensed User
Longtime User
Thanks again, it will be a 100 or so items queried from a database and then values appended to each 'row in the list' (so in effect an dynamic array.

And Happy New Year from Scotland when it comes in a few hours.
 
Upvote 0

tremara1

Active Member
Licensed User
Longtime User
query from db

I am not sure why you are using a user type, when you use a cursor to query the db the items you want can be accessed via loop and added to the listview directly with getString.
B4X:
lstlist.clear
       For i = 0 To Cursor.RowCount - 1
        Cursor.Position = i
      lstList.AddSingleLine(Cursor.getString("name") & " = " & Cursor.GetString("val"))
   
   Next
    Cursor.Close
In this example the name and val are field names in table you are querying.
Just thought it was a little easier.
 
Upvote 0

gmlogan

Member
Licensed User
Longtime User
Filling the listview is the last step I am trying to do. Basically I want to take a set of data from a DB query, pass it to some functions to do some calculations on the data based on some user entered values. Depending on user values would call differeht functions.
My example use named.pair and can do that with a Map object, actual task will have multiple values per key (items per row).
An array will do nicely but I cannot tell ahead of time length, I may do that anyway with an sql to get number or rows then Dim the table.
Thougth I could use a Python like 'structure', just experimenting
 
Upvote 0
Top