Storing string with Enums in VB.Net

Below is one of the ways to store/retrieve   string data type using Enums .

This is particularly useful in situations where  you have to deal with string values repeatedly in your project and don’t want to run into gotcha errors because you mistyped a string .

Imagine an  Event logger  where you log multiple error types (“connection error” ,”incorrect login details ”,”system error”).Its quite error prone to type those strings manually .So below is a sample of how you can do it in slightly better way

Public Class ErrorCode
Private Key As String

Public Shared ReadOnly SystemException As ErrorCode = New ErrorCode("201")
Public Shared ReadOnly DbAuthException As ErrorCode = New ErrorCode("202")

Private Sub New(key As String)
Me.Key = key
End Sub

Public Overrides Function ToString() As String
Return Me.Key
End Function
End Class

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s