Android Question Howto add a "type record" in a list ?

CR95

Active Member
Licensed User
I try to resume my issue in the following example :

Type TestType(Year As String)
Dim Rec As TestType
Rec.Initialize
Dim myList As List
myList.Initialize
Rec.Year = "1900"
myList.Add(Rec)
Rec.Year = "1950"
myList.Add(Rec)
Rec.Year = "2000"
myList.Add(Rec)
For i = 0 To myList.Size-1
Dim z As TestType
z = myList.Get(i)
Log(z)
Next
Log result -> all records are identical to the last loaded
[IsInitialized=true, Year=2000]
[IsInitialized=true, Year=2000]
[IsInitialized=true, Year=2000]
Please could you tell me what is wrong ?
 

LucaMs

Expert
Licensed User
Longtime User
You must create and initialize a new object of that type each time.
B4X:
Dim Rec As TestType
Rec.Initialize
Rec.Year = "1900"
myList.Add(Rec)

Dim Rec As TestType
Rec.Initialize
Rec.Year = "1950"
myList.Add(Rec)

To publish source code, use:
1698401426073.png
 
Upvote 0

Chris2

Active Member
Licensed User
If you hover over the type declaration the IDE will offer to make you a 'CreateType...' sub:
CreateSub.png

B4X:
Public Sub CreateTestType (Year As String) As TestType
    Dim t1 As TestType
    t1.Initialize
    t1.Year = Year
    Return t1
End Sub
Then you can simplify the process of creating & initialising TestType objects:
B4X:
Dim Rec As TestType = CreateTestType("1900")
myList.Add(Rec)

'Or put them straight in the list

myList.Add(CreateTestType("1900"))
 
Upvote 1

kimstudio

Active Member
Licensed User
Longtime User
@kimstudio
Yes. Why the Initialize is needed EACH TIME ?
I should say Dim & Initialize is needed each time.

I can only guess it using C++ way: an object with dim/initialize is created in the memory, there is a pointer pointing to this object in the memory, List.Add() will only add this pointer in list. Without dim/initialize the object's property's value is changed each time but it is the same object, so same pointer, List.Add() add three times same pointer to same object. Although the var name is same, each dim/initialize will create a new object/instance.
 
Upvote 0

CR95

Active Member
Licensed User
Thanks for the explanations
To be honest, I don't think I have the level to understand all the "internals". But I am happy to be able to use them.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thanks for the explanations
To be honest, I don't think I have the level to understand all the "internals". But I am happy to be able to use them.
It is not difficult. Custom types are like classes.

If you have created a class, then you must create objects of the type of that class, creating a variable and initializing it; only at that point will you have an object, which you could add to a List or any other structure (Map, for example).

It's the same thing with custom types.


The editor, as @Chris2 wrote, helps you by generating a function to create a new object of the type you created.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since we are beating this subject to death, here is a complete example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Type TestType(Year As String)
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Dim myList As List
    myList.Initialize
    myList.AddAll(Array(CreateTestType (1900), CreateTestType (1950), CreateTestType (2000)))
    
    For Each z As TestType In myList
        Log(z.Year)
    Next
End Sub

Public Sub CreateTestType (Year As String) As TestType
    Dim t1 As TestType
    t1.Initialize
    t1.Year = Year
    Return t1
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If the Type is not Initialize then the fields will remained as Null object.
If it has been initialized, the String field will have a default value of empty String.
This is my observation.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I don't like AddAll; this will limit the use of the List.
Why. If you do not like something, you have to explain why.
This still works:
B4X:
Dim myList As List
    myList.Initialize
    myList.AddAll(Array(CreateTestType (1900), CreateTestType (1950), CreateTestType (2000)))
    myList.Add(CreateTestType (2020))
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Why. If you do not like something, you have to explain why.
Uhm... because I seem to remember that until some time ago using AddAll the List became a static array, some List methods no longer worked (like InsertAt, RemoveAt).

Now they work; I think things have changed and, what's much worse, I think I've already written this in the past and gotten a response from Erel (who I'm about to look for)
 
Upvote 0
Top