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
Enumerations
- BruceSteers
- Posts: 1574
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Enumerations
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
BruceS
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Enumerations
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
- BruceSteers
- Posts: 1574
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Enumerations
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
BruceS
Re: Enumerations
Thanks, this is the thing I'm looking for.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
Witchi