List of Object

Carcas

Member
Licensed User
Longtime User
hi
I have searched the forum but have not found anything.

I would like an example of a list of objects

I would like to create a list of objects and then implement properties.

Example

if I have an array I can do this:

Dim a as oggetto
Dim x(10) as oggetto

a.initialize(10,10,10,10)------ '(x,y,g,h)

x(0) = a
x(0).g = 12 'IS POSSIBLE

if i have a list:

dim a as oggetto
dim b as list

a.initialize(10,10,10,10)
b.add(a)

b(0).g = 12 'ISNT POSSIBLE ?????? in visual basic 2010 is possible

you can declare a list of a certain type?

how should I do to implement the properties of the object in the list?

I apologize for my poor English

thanks much :)
 
Last edited:

agraham

Expert
Licensed User
Longtime User
You don't declare List as being of any type. Unlike an array a list is always a List of Object, just like an ArrayList in VB, and can hold any type of object all at the same time.

You have to use this because that is the way that Java works.
B4X:
b.Get(0).g
I think the reason is that is that, unlike the CLR, Java lacks operator overloading
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
You don't declare List as being of any type. Unlike an array a list is always a List of Object, just like an ArrayList in VB, and can hold any type of object all at the same time.

You have to use this because that is the way that Java works.
B4X:
b.Get(0).g
I think the reason is that is that, unlike the CLR, Java lacks operator overloading

isnt possible

b.get(0).g 'I've already tried

intellisense not found b.get(0).??????? -----nothing

if runs app i have this error

Parsing code. 0.00
Compiling code. Error
Error compiling program.
Error description: Unknown type: Object
Are you missing a library reference?
Occurred on line: 37
a.Get(0).Altezza
Word: altezza
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
this is my class

Sub Class_Globals 'Name Oggetto
Public X As Int
Public Y As Int
Public Altezza As Int
Public Larghezza As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (ax As Int, aY As Int, aAltezza As Int, aLarghezza As Int)
X=ax
Y=aY
Altezza = aAltezza
Larghezza = aLarghezza
End Sub

this is my problem solution:

Dim a As List
Dim b,d As Oggetto
Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("Prova")
a.Initialize
b.Initialize(0,0,100,100)
a.Add(b)
d = a.Get(0)

d.Altezza = 16
a.Set(0,d)


I used an object 'd' as a support and then I replaced the object with new properties on the list.
In this way, however, the original object has been destroyed

is not a good solution

there are better solutions?
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
:sign0013: I see know why b.get(0).g doesn't work. I've been doing too much Java recently and I'm thinking in Java not B4A.

Yes you need the assignment to a temporary variable if the original is not available so that the B4A compiler knows what type the object is. But you do not need the a.Set(0,d) because the list holds references to the actual object instance not the instance itself. So copying the reference from the list to a temporary variable and changing one of its fields changes the field in the actual instance. Try it.
 
Upvote 0
Top