How to use enum

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can define a custom type which will behave as an enum:
B4X:
Type SeasonsType (Summer As Int, Fall As Int, Winter As Int, Spring As Int)
Dim Seasons As SeasonsType
Seasons.Summer = 0:Seasons.Fall = 1:Seasons.Winter = 2:Seasons.Spring = 3
The Seasons object can now be used as an enum.
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
Here's a quick way to get a poor man's version of enumerated constants that work with the "Intellisense" (drop-down list) feature.

Add a new code module called say "C". Declare your "Constants" in this.

(Thanks to Erel for the suggestion of adding the CONST keyword.)

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Const NOCHANGE=0,GENERAL=1,PHONENUMBER=2, EMAILADDRESS=4, DEVICENAME=5 As Int
  
End Sub

Positives:

Now when you enter "C." you get a dropdown list with ALL of the declared items in the "C" module.

Drawbacks:

You get ALL of the variables declared in "C" in the drop down. I suppose you could add modules with different names if you didn't want them mixed up.
If there were a lot of enumerated values of one type it would work fairly well.

I'm not sure what the overhead is in having multiple modules. It does make it very easy to re-use the enumerations though!
 
Last edited:
Upvote 0
Top