• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

VB Equivalent to Java Enums

Simple enums in Java have simple equivalents in VB:

Java VB
public enum Simple
{
    FOO,
    BAR
}
Public Enum Simple
    FOO
    BAR
End Enum

More complex enums in Java unfortunately have exceedingly complex equivalents in VB:

Java VB
public enum Complex
{
    FOO("Foo"),
    BAR("Bar");

    private final String value;

    Complex(String enumValue)
    {
        this.value = enumValue;
    }

    public void InstanceMethod()
    {
        //...
    }
}
Imports System.Collections.Generic

Public NotInheritable Class Complex
    Public Shared ReadOnly FOO As New Complex("FOO", InnerEnum.FOO, "Foo")
    Public Shared ReadOnly BAR As New Complex("BAR", InnerEnum.BAR, "Bar")

    Private Shared ReadOnly valueList As IList(Of Complex) = New List(Of Complex)()

    Shared Sub New()
        valueList.Add(FOO)
        valueList.Add(BAR)
    End Sub

    Public Enum InnerEnum
        FOO
        BAR
    End Enum

    Public ReadOnly innerEnumValue As InnerEnum
    Private ReadOnly nameValue As String
    Private ReadOnly ordinalValue As Integer
    Private Shared nextOrdinal As Integer = 0

    Private ReadOnly value As String

    Friend Sub New(ByVal name As String, ByVal thisInnerEnumValue As InnerEnum, ByVal enumValue As String)
        Me.value = enumValue

        nameValue = name
        ordinalValue = nextOrdinal
        nextOrdinal += 1
        innerEnumValue = thisInnerEnumValue
    End Sub

    Public Sub InstanceMethod()
        '...
    End Sub

    Public Shared Function values() As IList(Of Complex)
        Return valueList
    End Function

    Public Function ordinal() As Integer
        Return ordinalValue
    End Function

    Public Overrides Function ToString() As String
        Return nameValue
    End Function

    Public Shared Function valueOf(ByVal name As String) As Complex
        For Each enumInstance As Complex In Complex.valueList
            If enumInstance.nameValue = name Then
                Return enumInstance
            End If
        Next
        Throw New System.ArgumentException(name)
    End Function
End Class


Use Java to VB Converter to convert from Java to VB.     

Additional resource: VB.NET and Java Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.