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

Mayost Sharon sharon at 455.co.il
Sun Apr 16 09:08:54 CEST 2023


---------- Original Message -----------
From: "Mayost Sharon" <sharon at 455.co.il>
To: Gambas Mailing List <user at lists.gambas-basic.org>
Sent: Sat, 15 Apr 2023 21:58:10 +0300
Subject: Re: [Gambas-user] class within a class with a key

> ---------- Original Message -----------
> From: Bruce Steers <bsteers4 at gmail.com>
> To: Gambas Mailing List <user at lists.gambas-basic.org>
> Sent: Fri, 14 Apr 2023 17:35:57 +0100
> Subject: Re: [Gambas-user] class within a class with a key
> 
> > 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
> > >>
> > >>
> ------- End of Original Message -------
> 
> Many thanks again
> on the attached code
> 
> I'm trying to understand
> I will report questions if I have any
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
------- End of Original Message -------

I checked your code

1. I wanted to get to:
gb_Class1.students.Add.Add("key_01", "Gambas", "Visual Basic")
not to
gb_Class1.Add("key_01", "Gambas", "Visual Basic")
It means that the KEY
will be in CLASS2 which is students

In gb_Class1 there will be only:
gb_Class1.students

In gb_Class1.students there will be only:
gb_Class1.students.Add

In gb_Class1.students[Key] there will be only:
gb_Class1.students[Key].student_01
gb_Class1.students[Key].student_02

in Gambas documentation  writes that the best example is:
gb.settings

he does a "virtual class"

I tried changing your code:
(new code attached)
But it gives me an error
I tried to understand and could not understand why

Many thanks
-------------- next part --------------
A non-text attachment was scrubbed...
Name: class_02.zip
Type: application/zip
Size: 19093 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230416/77ee5b40/attachment-0001.zip>


More information about the User mailing list