1

I would like a wau to organize and group a collection of strings in the same way as an enum. That is with a name used in code and a value. Since enums can only be created as integrals, I usually do like this:

**Public Structure SomeStrings
    Public Shared ReadOnly Property Something1 As String
        Get
              Return "Something1"            
        End Get
    End Property
    Public Shared ReadOnly Property Something2 As Color
        Get
            Return "Something2"
        End Get
    End Property
End Structure**

Is there a better way to do it?

SuppaiKamo
  • 285
  • 2
  • 5
  • 16

2 Answers2

4

Is there a better way?

Yes, use Shared ReadOnly fields instead of properties. You can even get proper IDE auto-completion like for real enums by using an undocumented XML doc tag:

''' <completionlist cref="SomeStrings"/>
Public Structure SomeStrings
    Public Shared ReadOnly Something1 As String = "Something1"
    ' etc.
End Structure

For more details, refer to my previous answer explaining this feature.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

You might want to look into creating resources.

An other option could be to use a dictionary.

Dim d As New Dictionary(Of Integer, String)

d.Add(1, "String 1")
d.Add(2, "String 2")

Console.WriteLine(d(2))
the_lotus
  • 12,668
  • 3
  • 36
  • 53