Set Statement

   
Set Statement

Syntax

 Set    [   statements   ]    [   variable   = Value ] End Set 
statements (optional)

Program code to be executed when the Property Set procedure is called

variable (optional; any (the data type of the property)

Typically, a Private variable to hold the property value

Value (optional; Keyword)

A keyword representing the value to be assigned to the property

Description

Defines a Set property procedure that sets a property value

Rules at a Glance

  • 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.

Example

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 

Programming Tips and Gotchas

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.

VB.NET/VB 6 Differences

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.

See Also

Get Statement, Property Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net