Search code examples
asp.netvb.netstringobjectshared

VB/ASP.Net: Change static String to own class - how?


I used to have a Public class with around 1800 Public Shared Strings in it - kinda like my constants library:

Public Shared ReadOnly NLS_ALREADY_IN_USE_QUERYNAME As String = "AlreadyInUseQueryName"

Now I want to store a little more info for each element and change them to "As MyClass" with an appropriate constructor that takes the String from my previously used definition:

Public Shared ReadOnly NLS_ALREADY_IN_USE_QUERYNAME As QNLSDefinition = New QNLSDefinition("AlreadyInUseQueryName", "Deutsch", "English")

The problem is that these "objects" are not instantiated automatically although they are Shared. String obviously is "immediately instantiated".

Any best practise around for this?


Solution

  • You can create a Constructor for that Class and you instantiate all Static variables there in your contructor just one time in your application:

    Class Constants 
    
       Public Shared ReadOnly NLS_ALREADY_IN_USE_QUERYNAME As QNLSDefinition
    
       'Constructor
       Sub New()
    
          NLS_ALREADY_IN_USE_QUERYNAME = New QNLSDefinition("AlreadyInUseQueryName",  "Deutsch", "English")
    
       End Sub
    
    End Class
    

    And then you go to your application constructor

    Sub New()
       InitializeComponent()
       'Instantiate shared variables
       Dim const as new Constants
     End Sub()