B4J Question Odd behaviour

Daestrum

Expert
Licensed User
Longtime User
(This is hard to explain so bear with me, also using the java 11 beta ide)

If I have an application that contains the following

A Main class
A Class module -- contains name as string and age as int and no code
A Code module -- contains no code at all just the default generated code

The main code creates a list that is populated with instances of the class (Person) from the class module with name and age set to values.

If I then list the contents of the list

B4X:
For Each P As Person in theList
Log(p)
Next

I would expect to see
[name=???,age=??]

What I actually get is
[name=???, age=??, main=null, theCodeModuleName=null]

Why is it adding the code module to the item ?

The more code modules you add, the more you get in the item (all the code module names and main).
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Tip: Use a custom type. Classinstances can not be saved in a list.
B4X:
Type Person(Name As String, Age as Int)
change your class and add a method to get the Type from it. It should return a instance of this type and you add this type to your list.
B4X:
Sub getPerson() As Person
    Dim p As Person
    p.Initialize
    p.Name = mName  'Name from Class variable'
    p.Age = mAge ' Variable from class too
    Return p
End Sub
Also add a method to your classs (or extend the initialization) to initialize your Class with a given Person type.
B4X:
sub Initialize(person As Person)
    mName = person.Name
    mAge = person.Age
end sub
 
Last edited:
Upvote 0
Top