Set Statement |
Set [ statements ] [ variable = Value ] End Set
Program code to be executed when the Property Set procedure is called
Typically, a Private variable to hold the property value
A keyword representing the value to be assigned to the property
Defines a Set property procedure that sets a property value
The Set statement can only be used within a Property...End Property construct.
The value assigned to the property is usually stored to a variable that's Private to the class. This protects the property value from modification other than by calling the Property Set procedure.
The Value keyword represents the value to be assigned to the property. This value must be of the same data type as the property.
The example code illustrates a class that has a simple property and a property array. The syntax documented above, rather than the "official" syntax (see the note in Section ), is used, since in our opinion it is much clearer and intuitive.
Public Enum WageConstants Rate = 0 Overtime = 1 Differential = 2 End Enum Public Class CEmployee Dim strName As String Dim decWage(2) As Decimal Public Property Name( ) As String Set(sName As String) strName = sName End Set Get Return strName End Get End Property Public Property Wage(iType As WageConstants) As Decimal Get Wage = decWage(iType) End Get Set decWage(iType) = Value End Set End Property End Class Module modMain Public Sub Main Dim oEmp As New CEmployee oEmp.Name = "Bill" oEmp.Wage(WageConstants.Rate) = CDec(15.00) oEmp.Wage(WageConstants.Overtime) = CDec(15.00 * 1.5) oEmp.Wage(WageConstants.Differential) = CDec(15.00 * .1) Console.WriteLIne(oEmp.Name) Console.Writeline(oEmp.Wage(WageConstants.Rate)) oEmp = Nothing End Sub End Module
An alternative syntax for the Set statement (though it happens to be the officially documented one, as well as the one used by Visual Studio) is:
Set([ByVal] var As Type ) [ statements ] [ variable = var ] End Set
Here var is a variable representing the value to be assigned to the property, and Type is the data type of var . Type must be the same as the data type of the Property statement.
The Property Let and Property Set statements in VB 6 correspond to the Set statement in VB.NET. Though the purpose and basic operation of these constructs are identical, the syntax of the VB.NET construct is vastly simplified and more intuitive.
Get Statement, Property Statement