Enumerations

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
Witchi
Posts: 11
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Enumerations

Post by Witchi »

Hi,
Is there a construct in Gambas like Enumerations (as known as in Java or C++)? Or should I define a class with constants to emulate that?

Thanks
Witchi
User avatar
BruceSteers
Posts: 1575
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Enumerations

Post by BruceSteers »

Witchi wrote: Sunday 19th February 2023 2:02pm Hi,
Is there a construct in Gambas like Enumerations (as known as in Java or C++)? Or should I define a class with constants to emulate that?

Thanks
Witchi
absolutely Anything can be made into an array and enumerated

Just like this for an example object called MyObject
Dim MyObjectArray As New MyObject[]  ' initialize an empty Array of MyObject called MyObjectArray.

' Add some MyObjects to the array
MyObjectArray.Add(MyObject1)
MyObjectArray.Add(MyObject2)
MyObjectArray.Add(MyObject3)

' Enumerate through the list..
For Each hMyObject As MyObject In MyObjectArray
  Print hMyObject.Data1
  Print hMyObject.Data2
Next
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Enumerations

Post by cogier »

I can't get Bruce's code to work. This is a little simpler if it helps.

Public Sub Form_Open()

  Dim MyArray As Integer[] = [5, 4, 7, 8, 9, 1, 2, 0, 3, 6, 5, 8, 7]
  Dim iInt As Integer

  For Each iInt In MyArray
    Print iInt
  Next

End


Image
User avatar
BruceSteers
Posts: 1575
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Enumerations

Post by BruceSteers »

cogier wrote: Sunday 19th February 2023 3:36pm I can't get Bruce's code to work. This is a little simpler if it helps.
Well it wasn't really code it was an example. you'd have to make a MyObject.class and give it Data1/Data2 properties :)

But your code is as good as any.

Here's some other ways to enumerate.
Public Sub Form_Open()
 
  Dim MyArray As Integer[] = [5, 4, 7, 8, 9, 1, 2, 0, 3, 6, 5, 8, 7]
  Dim iCount As Integer
 
  For iCount = 0 To MyArray.Max
    Print MyArray[iCount]
  Next
 
End


see https://gambaswiki.org/wiki/comp/gb/enum for how to make your own class have enumeration.


Or do you mean declaring constant variables that increase in number? that sort of Enum?
You can do that too.
See here
https://gambaswiki.org/wiki/lang/enumdecl

Public Enum This, That, TheOther
'This will be 0, that 1 and so on

or...
Public Enum This = 10, That, TheOther
' This will be 10, That 11 and so on
If at first you don't succeed , try doing something differently.
BruceS
Witchi
Posts: 11
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Re: Enumerations

Post by Witchi »

BruceSteers wrote: Sunday 19th February 2023 3:49pm Or do you mean declaring constant variables that increase in number? that sort of Enum?
You can do that too.
See here
https://gambaswiki.org/wiki/lang/enumdecl

Public Enum This, That, TheOther
'This will be 0, that 1 and so on

or...
Public Enum This = 10, That, TheOther
' This will be 10, That 11 and so on
Thanks, this is the thing I'm looking for.
Witchi
Post Reply