Container for constants in VB.NET
Say I have some constants that depends on one another, and I decided to keep them together in a container instead of keeping it as individual constants in a class.
I thought use a Structure
for that scope, but the compiler forces me to declare a private member for that structure.
' Compile error: at least one private member is needed.
Private Structure BandSizes
Const BandHeight As Short = HourHeight + 20
Const HourHeight As Short = HalfHourHeight + 20
Const HalfHourHeight As Short = LineHeight + PictureHeight + 20
Const PictureHeight As Short = 20
Const LineHeight As Short = StopHeight + 10
Const LineWidth As Short = 50
Const Stop开发者_Go百科Height As Short = 30
End Structure
As I have only a few integer constants, should I create a shared (static) class?
Platform: VB.NET (.NET 2)
In my opinion, the best option for this purpose is to create a (private) class with only shared, static members, and constants. You don't need to create an object and you can control accessibility as you want.
Private NotInheritable Class BandSizes
Public Const BandHeight As Short = HourHeight + 20
Public Const HourHeight As Short = HalfHourHeight + 20
Public Const HalfHourHeight As Short = LineHeight + PictureHeight + 20
Public Const PictureHeight As Short = 20
Public Const LineHeight As Short = StopHeight + 10
Public Const LineWidth As Short = 50
Public Const StopHeight As Short = 30
Private Sub New()
End Sub
End Class
Note: NotInheritable
is needed in the declaration of the class, because it is what the compiler would produce as CIL when you use a Module. I prefer a "standard-.NET"-way over the Visual Basic-only stuff. Besides, you have more control over its accessibility and you can make it as innerclass. This is in fact the VB.NET counterpart to a C# static class.
Since it's VB.NET, if they are truly global constants and their name by themselves is enough, you can also create a Module to keep them in.
Also, be aware that if these constants are accessed from external assemblies there can be issues if the values of these constants can ever change. So if these were public and put in a class library for example it might be better to have them as ReadOnly rather than constant.
Instead of making it "const", I would recommend you to have a look at the article Constants in .NET and then decide that you should use "const" or you should go with "readonly".
An enum
might be a better option. Not sure about VB syntax, but C# would be:
internal enum BandSizes {
BandHeight = HourHeight + 20,
HourHeight = HalfHourHeight + 20,
HalfHourHeight = LineHeight + PictureHeight + 20,
PictureHeight = 20,
LineHeight = StopHeight + 10,
LineWidth = 50,
StopHeight = 30,
}
(NB. if this is an namespace scope, internal
is the most restrictive access, but private
is possible if a member of a class or structure.)
EDIT: Here's the VB version:
Friend Enum BandSizes
BandHeight = HourHeight + 20
HourHeight = HalfHourHeight + 20
HalfHourHeight = LineHeight + PictureHeight + 20
PictureHeight = 20
LineHeight = StopHeight + 10
LineWidth = 50
StopHeight = 30
End Enum
Where Friend
is the VB for internal
(and the same limitation on Private
is only possible for type members).
精彩评论