[Gambas-user] Adding Classes to Collections... we can't do this?

sbungay sbungay at ...981...
Mon May 30 21:56:05 CEST 2005


   Ahhh OK. Will try that and see. I prefer collections over arraya... 
especially because of the unique key feature.... very handy.

Benoit Minisini wrote:
> On Monday 30 May 2005 00:22, sbungay wrote:
> 
>>   OK, I'm coming from the VB 6.0 world and I know I know this is not
>>VB... I have a question anyway about adding classes to collections. Um..
>>why can't we do this? So far al I can do is add each item in the class
>>to the collection... but that is a royal pain in the butt. What I want
>>to do is something like this.
>>
>>'*********** BEGIN SAMPLE CODE ***************************************
>>'*** This does not work...
>>Dim MyCollection as New Collection
>>
>>' The Class has two integer values in it.. X and Y (just for example)
>>Dim MyClass as XYClass
>>
>>MyClass = new XYClass
>>
>>With MyClass
>>      .X = 10
>>      .Y = 10
>>End With
>>
>>MyCollection.Add(MyClass)
>>
>>MyClass = NULL  ' Is this equiv. to the VB "Set MyClass = Nothing"
>>
>>
>>'*** This DOES work but is kludgy
>>Dim MyCollection as New Collection
>>
>>' The Class has two integer values in it.. X and Y (just for example)
>>Dim MyClass as XYClass
>>
>>MyClass = new XYClass
>>
>>With MyClass
>>      .X = 10
>>      .Y = 10
>>End With
>>
>>MyCollection.Add(MyClass.X, MyClass.Y)
>>
>>MyClass = NULL  ' Is this equiv. to the VB "Set MyClass = Nothing"
>>
>>
>>'*********** END SAMPLE CODE ***************************************
>>
>>   Now froma programming standpoint I like the first way better because
>>I  simply add the created class to the collection. Wonderful, less
>>typing for me.
>>   Does the problem stem from collection only being able to hold Variant
>>types?
>>
> 
> 
> Gambas Collections are not exactly like in VB. The first argument of Add() is 
> the key, the second is the value. They are both mandatory, contrary to VB.
> 
> If you don't need a key, then use an array instead of a Collection:
> --> Dim MyCollection as New Object[]
> 
> Regards,
> 




More information about the User mailing list