Question constants

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
i have a bunch of constants that i need to access all the time and i want to access it like mData.constantA . What is the best way to do it? I tried making a Structure Data.

VB.NET:
   Dim mData as Data
    Structure Data
        Public ReadOnly Property constantA() As Integer
            Get
                Return 123
            End Get
        End Property
          Public ReadOnly Property constantB() As Integer
            Get
                Return 234
            End Get
        End Property
  End Structure
but then it say Structure Data must contain at least one instance member variable or Event declaration. So i added a useless
VB.NET:
Public abc As Integer
And its working. What would be a better way to do this?
 
Last edited:
To create a type with nothing but Shared members you cannot use a structure. You would have to use a class or you could use a module. A module is essentially a class with nothing but Shared members. The C# equivalent of a VB module is a static class, which is literally a class with nothing but static (the C# equivalent of Shared) members, so a module is the appropriate choice.
 
For this, is there a different between using
VB.NET:
        Public Shared ReadOnly constantA As Integer = 123
and
VB.NET:
     Public ReadOnly Property constantA() As Integer
            Get
                Return 123
            End Get
        End Property
 
For this, is there a different between using
VB.NET:
        Public Shared ReadOnly constantA As Integer = 123
and
VB.NET:
     Public ReadOnly Property constantA() As Integer
            Get
                Return 123
            End Get
        End Property

There is no practical difference. Really, you should probably just do this:
VB.NET:
Public Const constantA As Integer = 123
If the value is constant then it should be declared as a Const. Constants are inherently Shared, like module members.
 
module

after i setup the module the constants became all shared and can be access with just its name.

VB.NET:
Module mData
   Public Const constantA as integer = 123
   Public Const constantB as integer = 234
End Module

i could access the constant by just using constantA without using mData.constantA . So i think changing it back to a class is more what i need. So if Constants are inherently Shared. Why by changing it from Module to Class making the constantA only accessable by using mData.constantA?
 
VB.NET modules are designed to behave in the same way that VB6 modules did, which is why you don't have to qualify module members with the module name. That doesn't mean that you can't qualify them though. If you want to qualify a module member with the module name then you're free to do so, so that's not a reason not to use a module. You could use a class if you wanted to but there's no way to prevent a class being instantiated, which is inappropriate in this case. You might say that you just won't instantiate it so it's not a problem, but how is that any different to just not using module members unqualified?
 
Back
Top