I am using Window Application for my project. There is situation where i need to define string enum and using it in my project.
i.e.
Dim PersonalInfo As String = "Personal Info" Dim Contanct As String = "Personal Contanct" Public Enum Test PersonalInfo Contanct End Enum
Now i want value of that variable PersonalInfo and Contract as "Personal Info" and "Personal Contanct".
How can i get this value using ENUM? or anyother way to do it.
Thanks in advance...
For non-integer values, Const
in a Structure
(or Class
) can be used instead:
Structure Test Const PersonalInfo = "Personal Info" Const Contanct = "Personal Contanct" End Structure
or in a Module
for direct access without the Test.
part:
Module Test Public Const PersonalInfo = "Personal Info" Public Const Contanct = "Personal Contanct" End Module
In some cases, the variable name can be used as a value:
Enum Test Personal_Info Personal_Contanct End Enum Dim PersonalInfo As String = Test.Personal_Info.ToString.Replace("_"c, " "c) ' or in Visual Studio 2015 and newer: Dim Contanct As String = NameOf(Test.Personal_Contanct).Replace("_"c, " "c)
You could just create a new type
''' <completionlist cref="Test"/> Class Test Private Key As String Public Shared ReadOnly Contact As Test = New Test("Personal Contanct") Public Shared ReadOnly PersonalInfo As Test = New Test("Personal Info") Private Sub New(key as String) Me.Key = key End Sub Public Overrides Function ToString() As String Return Me.Key End Function End Class
and when you use it, it kinda looks like an enum:
Sub Main DoSomething(Test.Contact) DoSomething(Test.PersonalInfo) End Sub Sub DoSomething(test As Test) Console.WriteLine(test.ToString()) End Sub
output:
Personal Contanct
Personal Info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With