9.11. Const and ReadOnly MembersVisual Basic allows you to create constantsmembers whose values cannot change during program execution. To create a constant instance variable of a class, declare that member as either Const or ReadOnly. An instance variable declared as Const must be initialized in its declaration; an instance variable declared as ReadOnly can be initialized either in its declaration or in the class constructor. Const values must be initialized at compile time; whereas ReadOnly values are not initialized until runtime. Neither a Const nor a ReadOnly value can be modified once initialized. Error-Prevention Tip 9.4
Common Programming Error 9.7
Common Programming Error 9.8
Const members must be initialized at compile time; thus, they can be initialized only to constant values, such as integers, string literals, characters and other Const members. Constant members with values that cannot be determined at compile time must be declared ReadOnly. Recall that a ReadOnly member can be assigned a value only once, either when it is declared or within the class's constructor (the Shared constructor for Shared ReadOnly members and a non-Shared constructor for non-Shared ReadOnly members). Common Programming Error 9.9
Common Programming Error 9.10
Common Programming Error 9.11
Common Programming Error 9.12
Class CircleConstants with Const and ReadOnly Instance VariablesClass CircleConstants (Fig. 9.12) demonstrates constants. Line 4 creates constant PI using keyword Const and assigns to PI the Double value 3.14159an approximation of π. We could have used pre-defined constant PI of class Math (Math.PI) as the value, but we wanted to demonstrate how you can create your own Const instance variables. The com-piler must be able to determine a Const's value to be able to initialize a Const instance variable with that value. The value 3.14159 is acceptable (line 4), but the expression Convert.ToDouble("3.14159") generates a compilation error if used in place of that value. Although this expression uses a constant value (String literal "3.14159") as an argument and produces the same numeric value, a compilation error occurs, because the compiler cannot evaluate executable code (such as a method call). This restriction is lifted with ReadOnly members, which are initialized at runtime. Note that line 9 assigns the value of the constructor parameter radiusValue to ReadOnly member RADIUS at runtime. We could also have used a method call, such as Convert.ToDouble, to assign a value to this ReadOnly member. Figure 9.12. Constants used in class CircleConstants.
Using Const and ReadOnlyModule ConstAndReadOnly (Fig. 9.13) demonstrates the Const and ReadOnly values. Line 5 creates a Random object that is used in line 8 to generate a random Integer in the range 120 that corresponds to a circle's radius. This enables each program execution to produce different output. The random value is passed to the CircleConstants constructor to initialize a CircleConstants object. Lines 1113 access the ReadOnly variable RADIUS through a reference to object circle, and compute and display the circle's circumference. This calculation employs the Public Const member PI, which we access in line 13 through its class name CircleConstants. Recall that Const members are implicitly Shared; thus we can access a Const member with its class name followed by the dot separator even when no objects of the class are present. Figure 9.13. Const and ReadOnly class members demonstration.
ReadOnly and WriteOnly PropertiesRecall from Chapter 4 that not all properties need to have Get and Set accessorsa property with only a Get accessor is called a read-only property and must be declared using keyword ReadOnly; a property with only a Set accessor is called a write-only property and must be declared using keyword WriteOnly. |