Where should I put my global constants in a .NET library?
constants, vb.net
Solution
These are constants. This means that no one can change their value. You should ask yourself what these constants represent. If they are pieces of global information needed everywhere in your program then there is no objection to put them in a global shared class. For a better understanding of their meaning I will comment them with XML comments and go ahead with coding.
Problem
I'm working with an API that spits out a lot of data in name=value format. At first I processed everything by doing simple string comparisons: ``` Sub ProcessData(ByVal name As String, ByVal value As String) If name = "thisname" Then DoThis(value) ElseIf name = "thatname" Then DoThat(value) End If End Sub ``` But with over 20 different possible names to process, this quickly became hard to maintain. My next step was to move the strings over to constants defined in a private sub-class: ``` Private Class Parameters Private Sub New() End Sub Public Const ThisName As String = "thisname" Public Const ThatName As String = "thatname" End Class ``` And my method would look like this: ``` Sub ProcessData(ByVal name As String, ByVal value As String) If name = Parameters.ThisName Then DoThis(value) ElseIf name = Parameters.ThatName Then DoThat(value) End If End Sub ``` This was already a huge leap forward, but now I find myself in a position where I need to be able to use these constants in other classes. I'm hesitant about moving them to a global class, but I just don't see another option. Where do global constants go?