Changes to Properties
Properties have been modified in Visual Basic .NET to use a unified approach to ensure that modifiers to property methods are applied uniformly to the getter and setter methods. Whereas VB6 implemented the property getter and setter as two separate property methods , Visual Basic .NET groups properties into a single block with the getter and setter as nested elements.
Unified Property Statement in Visual Basic .NET
The basic, unified property statement in Visual Basic .NET has an outer property block and an inner Get and Set block.
Private Property Dummy() As Integer Get End Get Set(ByVal Value As Integer) End Set End Property
The As clause in the property header defines the property's data type. Dummy is an Integer property. Treat the Get and End Get block as the property getter; it returns the underlying field value. Treat the Set block as the property setter; it sets the underlying field value. The argument passed to a property when the property is used as an l-value is the parameter Value. (Notice that you do not have to indicate the set parameter between the parentheses in the property header, represented by the empty parentheses.)
Property Let Is Not Supported
You do not need to distinguish between value type and reference type assignment in Visual Basic .NET. The result is that all assignments are accomplished with the assignment operator (=) and Set is never required. Set only appears in a property setter block in Visual Basic .NET.
Because there is no distinction between reference type and value type assignment, the Let keyword is no longer needed and is not supported in Visual Basic .NET.
Default Properties Cannot Be Shared or Private
Visual Basic .NET requires that Default properties must not be Shared or Private members .
Default Properties Must Take Arguments
Any property could be a Default property in VB6. Because the Set keyword was used for object assignment in VB6, VB6 could distinguish between default property usage and object assignment by the presence or absence of the keyword Set.
Because Set is not supported in an assignment operation, Visual Basic .NET requires an alternate queue indicating when the user wants to use a default property and when they mean object assignment. The new visual queue is the use of parentheses and a parameter, in conjunction with the property name . Listing A.1 demonstrates an example of a default property. (Refer to Figure A.3, illustrating how IntelliSense technology helps you use the default property.)
Figure A.3. When we add parentheses after the reference to self, Me, IntelliSense displays the default property syntax example as shown.
Listing A.1 A basic, parameterized default property
1: Default Public Property Dummy(ByVal Index As Integer) As Integer 2: Get 3: MsgBox(Index) 4: End Get 5: Set(ByVal Value As Integer) 6: MsgBox(Index) 7: End Set 8: End Property
Default properties must be parameterized. Generally the parameter plays the role of index to some underlying collection. In the property header the parameter is represented by the Index argument, and the keyword Default indicates that Dummy is the default property. The Dummy property is invoked in the following manner: Object ( index ) = IntegerValue or IntegerValue = Object ( index ). The first example uses Dummy as an l-value, invoking the property setter, and the second example uses the Dummy as an r-value, invoking the property getter. (Refer to the section on "Using Default Properties," in Chapter 7, "Creating Classes," for an extensive discussion of the default property idiom.)
Property Arguments Cannot Be Passed ByRef
Visual Basic .NET allows properties to be passed to methods ByRef, enabling a property to be changed within the called procedure (refer to "Passing Properties by Reference" earlier in this appendix).
Allowing arguments to properties to be passed ByRef would allow the property methods to change the argument, resulting in quirky behavior. Property arguments are immutable from the view of the caller.