B4J Code Snippet [B4X] Functional Implementation of vb.Net style Enum

In a recent thread, https://www.b4x.com/android/forum/threads/the-power-of-b4x.132226/
there was mention of the 'Enum' construct in vb.Net.

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/enum-statement

I did use it in vb but I hadn't really missed it in B4X. However, I thought I would explore the many ways it can be implemented in B4X.
You will probbaly have your own favorite method, but here is the one I like most.

I timed it and it takes about 5 minutes to create a new Enum.
It uses a SmartString and a copy and paste to get the Enumeration of variable names and values.
No need for Reflection or JavaObject.

This as a standard class 'EggSizeEnum' Other examples are in the attached .zip file.

B4X:
Sub Class_Globals
    Public names As List
    Public values As List

    Public Jumbo As Int = 0
    Public ExtraLarge As Int = 1
    Public Large As Int = 2
    Public Medium As Int = 3
    Public Small As Int = 4
  
    'copy above lines to inside the SmartString below - the names and values will be extracted
    Private def As String = $"
    Public Jumbo As Int = 0
    Public ExtraLarge As Int = 1
    Public Large As Int = 2
    Public Medium As Int = 3
    Public Small As Int = 4
    "$
End Sub

'The following can be used (copied) for each Enum - it initializes and loads 'names()' and 'values()' without having to retype these
Public Sub Initialize
    names.Initialize
    values.Initialize
    Dim v() As String = Regex.Split(CRLF, def)
    For Each t As String In v
        t = t.Trim
        If t.contains("Public") Then
            names.Add(t.SubString2(t.IndexOf("Public") + 7, t.IndexOf2(" ", 7)))
            values.Add(t.SubString(t.IndexOf("=") + 2).trim.Replace(QUOTE, ""))
        End If
    Next
End Sub

B4X:
Sub Process_Globals
    Public eggSize As EggSizeEnum
End Sub

Sub AppStart (Args() As String)
    eggSize.Initialize
  
    'After the above line, the Enum work pretty much like the vb.Net version
    'The items aren't Constants - not supercritical with current memory and processor speed
    'The items aren't read only - which in some cases may be an advantage
    'To manage many Enums, you can put them all in a custom B4X Library
  
    Dim size As Int = 2
    Select size
        Case eggSize.Jumbo: Log("Jumbo")
        Case eggSize.ExtraLarge: Log("ExtraLarge")
        Case eggSize.Large: Log("Large")
        Case eggSize.Medium: Log("Medium")
        Case eggSize.Small: Log("Small")
        Case Else: Log("Error - Invalid size")
    End Select

    For i = 0 To eggSize.Names.Size - 1
        Log(eggSize.Names.Get(i) & TAB & eggSize.Values.Get(i))
    Next
End Sub
 

Attachments

  • enumsExample.zip
    2.5 KB · Views: 197

LucaMs

Expert
Licensed User
Longtime User
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
Yes, that why I titled this "Functional Implementation". They are Functional constants. Variables that have values that do not change during execution. They can, but don't need to. The origin of using constants is related to constrained memory, streamlined compilation, and optimized execution. Not so critical anymore.

I this case it also "has a name, an underlying data type, and a set of members"
The method avoids writing "literals in the source code and groups them by categories/logic."
The enumExample.zip shows that it works almost the same as the examples in the vb.Net link.

You probably have your own favorite method for grouping "unchanging values", but I like this one.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
If you need to constrain the items, you can add the Const modifier to the Class Globals declarations, this makes them read-only.

B4X:
Sub Class_Globals
    Public names As List
    Public values As List

    Public Const Jumbo As Int = 0
    Public Const ExtraLarge As Int = 1
    Public Const Large As Int = 2
    Public Const Medium As Int = 3
    Public Const Small As Int = 4
    
    'copy above lines to inside the SmartString below - the names and values will be extracted
    Private def As String = $"
    Public Const Jumbo As Int = 0
    Public Const ExtraLarge As Int = 1
    Public Const Large As Int = 2
    Public Const Medium As Int = 3
    Public Const Small As Int = 4
    "$
End Sub

'A copy of the following can be re-used in all Enum(s) - it initializes and loads 'names()' and 'values()' without having to retype these
Public Sub Initialize
    names.Initialize
    values.Initialize
    Dim v() As String = Regex.Split(CRLF, def)
    For Each t As String In v
        If t.contains("Public") Then
            t = t.replace("Public ", "").replace("Const ", "").replace("As ", "").Replace(QUOTE, "").Trim
            names.Add(t.SubString2(0, t.IndexOf(" ")))
            values.Add(t.SubString(t.IndexOf("=") + 1))
        End If
    Next
End Sub
 
Top