VB.NET Constants
In this article I will explain about constants.
A constant is a variable modifier that represents a constant value, a value that can be computed at compile-time. A constant declaration introduces one or more constants of a given type.
A constant declaration can declare multiple constants (as in Listing 5.18) in a process that is equivalent to multiple declarations of single constants with the same attributes, modifiers, and type.
Listing 5.18: Constants Example 1
Class A
Public Const X As Integer = 1, Y As Integer = 2, Z As Integer = 3
End Class
Class B
Public Const X As Integer = 1
Public Const Y As Integer = 2
Public Const Z As Integer = 3
End Class
Evaluating constant values in complex class hierarchies can be a bit confusing and warrants closer examination.
The declaration of constants can depend on other constants within the same program (see Listing 5.19) as long as the dependencies are not of a circular nature. The .NET framework automatically arranges constant declarations to evaluate the declarations in the appropriate order.
Listing 5.19: Constants Example 2
Class A
Public Const X As Integer = B.Z + 1
Public Const Y As Integer = 10
End Class
Class B
Public Const Z As Integer = A.Y + 1
End Class
In this example, the compiler first evaluates Y, then Z, and finally X, producing the values 10, 11, and 12. Constant declarations may depend on constants from other programs, but such dependencies are only possible in one direction. Referring to the example above, if A and B were declared in separate programs, it would be possible for A.X to depend on B.Z; however, B.Z could not then depend on A.Y.
A static read-only field is useful when a symbolic name for a constant value is desired for example, when the type of the value is not permitted in a constant declaration or when the value cannot be computed at compile-time. Constants and read-only fields have different binary versioning semantics. When an expression references a constant, the value of the constant is obtained at compile-time. When an expression references a read-only field, the value of the field is not obtained until runtime.
Listings 5.20, 5.21, and 5.22 present a solid example of the difference between const and read-only in real-world applications.
Listing 5.20: ConstOut.vb , Constant Usage Example
Imports System
Public Class A
Public Const X As Integer = 123
End Class
Listing 5.21: ReadOnlyOut.vb, ReadOnly Usage Example
Imports System
Public Class B
Public Shared ReadOnly X As Integer = 123
End Class
Listing 5.22: ReadOnlyConst.vb, ReadOnly Const User Class Example
Imports System
Public Class MyTest
Public Shared Sub Main()
Console.WriteLine("A.X value = {0}", A.X)
Console.WriteLine("B.X value = {0}", B.X)
End Sub
End Class
The code in these three examples produces the display shown in Figure 5.7.
Figure 5.7: Screen Output Generated from Listings 5.20, 5.21, and 5.22
If you modify X in ConstOut.vb, then you have to compile both ConstOut.vb and ReadOnlyConst.vb consecutively. But if you modify X in ReadOnlyOut.vb, then you only need to compile ReadOnlyOut.vb; there is no need to recompile MyTest.vb. However, the execution speed of const is better than that of the static read-only.