Enumerations

Post

Posted
Rating:
#1 (In Topic #994)
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

Witchi said

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

Code (gambas)

  1. Dim MyObjectArray As New MyObject[]  ' initialize an empty Array of MyObject called MyObjectArray.
  2.  
  3. ' Add some MyObjects to the array
  4. MyObjectArray.Add(MyObject1)
  5. MyObjectArray.Add(MyObject2)
  6. MyObjectArray.Add(MyObject3)
  7.  
  8. ' Enumerate through the list..
  9. For Each hMyObject As MyObject In MyObjectArray
  10.   Print hMyObject.Data1
  11.   Print hMyObject.Data2
  12.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
I can't get Bruce's code to work. This is a little simpler if it helps.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim MyArray As Integer[] = [5, 4, 7, 8, 9, 1, 2, 0, 3, 6, 5, 8, 7]
  4.   Dim iInt As Integer
  5.  
  6.   For Each iInt In MyArray
  7.     Print iInt
  8.   Next
  9.  

<IMG src="https://www.cogier.com/gambas/Enum1.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

cogier said

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.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim MyArray As Integer[] = [5, 4, 7, 8, 9, 1, 2, 0, 3, 6, 5, 8, 7]
  4.   Dim iCount As Integer
  5.  
  6.   For iCount = 0 To MyArray.Max
  7.     Print MyArray[iCount]
  8.   Next
  9.  
  10.  

see /comp/gb/enum - Gambas Documentation 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
/lang/enumdecl - Gambas Documentation

Code (gambas)

  1. Public Enum This, That, TheOther
  2. 'This will be 0, that 1 and so on
  3.  
or…

Code (gambas)

  1. Public Enum This = 10, That, TheOther
  2. ' This will be 10, That 11 and so on
  3.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee

BruceSteers said

Or do you mean declaring constant variables that increase in number?  that sort of Enum?
You can do that too.
See here
/lang/enumdecl - Gambas Documentation

Code (gambas)

  1. Public Enum This, That, TheOther
  2. 'This will be 0, that 1 and so on
  3.  
or…

Code (gambas)

  1. Public Enum This = 10, That, TheOther
  2. ' This will be 10, That 11 and so on
  3.  

Thanks, this is the thing I'm looking for.
Witchi
Online now: No Back to the top
1 guest and 0 members have just viewed this.