[Gambas-user] class within a class with a key

Bruce Steers bsteers4 at gmail.com
Fri Apr 14 18:35:57 CEST 2023


On Fri, 14 Apr 2023 at 17:26, Bruce Steers <bsteers4 at gmail.com> wrote:

> use Export to expose functions.
>
> I added the _get command to Class1.class
>
> now something like this works...
>
>   Print gb_Class1["key_01"].student_01
>   Print gb_Class1["key_01"].student_02
>   Print gb_Class1["key_02"].student_01
>   Print gb_Class1["key_02"].student_02
>
>

I also added a Keys String[] array to store the array positions.
So each time it adds an item to the Class2[] Array it also adds the key to
the Keys list.

If you plan to remove items from the students[] array then you must remove
the item from the Keys list too.
Something like this in Class1.class ....

Public Sub Remove(Key As String)

  Dim iPos As Integer = Keys.Find(Key)
  If iPos = -1 Then Error.Raise("Key not found, " & Key)
  $students.Remove(iPos)
  Keys.Remove(iPos)

End


BruceS






> On Fri, 14 Apr 2023, 16:18 Mayost Sharon, <sharon at 455.co.il> wrote:
>
>> ---------- Original Message -----------
>> From: Bruce Steers <bsteers4 at gmail.com>
>> To: Gambas Mailing List <user at lists.gambas-basic.org>
>> Sent: Fri, 14 Apr 2023 14:56:09 +0100
>> Subject: Re: [Gambas-user] class within a class with a key
>>
>> > On Fri, 14 Apr 2023 at 13:55, Mayost Sharon <sharon at 455.co.il> wrote:
>> >
>> > > Hello
>> > > I need help on how to make a class within a class
>> > > Attached is an example that gives me an error because I'm missing
>> > > something that I can't figure out what to do
>> > >
>> > > I want to reach a state of:
>> > > Print gb_Class1.students["key_01"].student_01
>> > > Print gb_Class1.students["key_01"].student_02
>> > >
>> > >
>> > > Thank you
>> > >
>> > > ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>> > >
>> >
>> > to use Keys like a collection you need to use Collection datatype.
>> > Your Class2.class is not coded to work like a collection with Keys so it
>> > does not work as you expect.
>> >
>> > I got your code working by making the Class1.class  $students a
>> collection
>> > of Class2 objects.
>> > Class2.class has no functions just the student_01 student_02 properties.
>> >
>> > BruceS
>> ------- End of Original Message -------
>>
>> First of all thank you very much
>>
>> 1.
>> When I type "." In the end so that it completes
>> gb_Class1.students.
>> This completes a collection list
>> I wanted it to complement only what is in class 1
>> .Add
>> .students
>>
>> 2.
>> When I type "." In the end so that it completes
>> gb_Class1.students["key_01"].
>> It does not complete anything
>> Because it understands that it is a Variant variable
>> I wanted it to complement only what is in class 2
>> .student_01
>> .student_02
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230414/48bb74c6/attachment-0001.htm>


More information about the User mailing list