Set Statement (Visual Basic)
Set property procedure used to assign a value to a property.
[ <attributelist> ] [ accessmodifier ] Set (ByVal value [ As datatype ]) [ statements ] End Set
Optional. See Attribute List.
Optional on at most one of the
Set statements in this property. Can be one of the following:
Required. Parameter containing the new value for the property.
Option Strict is
On. Data type of the
value parameter. The data type specified must be the same as the data type of the property where this
Set statement is declared.
Optional. One or more statements that run when the
Set property procedure is called.
Required. Terminates the definition of the
Set property procedure.
Every property must have a
Set property procedure unless the property is marked
Set procedure is used to set the value of the property.
Visual Basic automatically calls a property's
Set procedure when an assignment statement provides a value to be stored in the property.
Visual Basic passes a parameter to the
Set procedure during property assignments. If you do not supply a parameter for
Set, the integrated development environment (IDE) uses an implicit parameter named
value. The parameter holds the value to be assigned to the property. You typically store this value in a private local variable and return it whenever the
Get procedure is called.
The body of the property declaration can contain only the property's
Set procedures between the Property Statement and the
End Property statement. It cannot store anything other than those procedures. In particular, it cannot store the property's current value. You must store this value outside the property, because if you store it inside either of the property procedures, the other property procedure cannot access it. The usual approach is to store the value in a Private variable declared at the same level as the property. You must define a
Set procedure inside the property to which it applies.
Set procedure defaults to the access level of its containing property unless you use
accessmodifier in the
Mixed Access Levels. If you are defining a read-write property, you can optionally specify a different access level for either the
Setprocedure, but not both. If you do this, the procedure access level must be more restrictive than the property's access level. For example, if the property is declared
Friend, you can declare the
Private, but not
If you are defining a
Setprocedure represents the entire property. You cannot declare a different access level for
Set, because that would set two access levels for the property.
Returning from a Property Procedure. When the
Setprocedure returns to the calling code, execution continues following the statement that provided the value to be stored.
Returnstatements cause an immediate exit from a property procedure. Any number of
Returnstatements can appear anywhere in the procedure, and you can mix
The following example uses the
Set statement to set the value of a property.
Class propClass Private propVal As Integer Property prop1() As Integer Get Return propVal End Get Set(ByVal value As Integer) propVal = value End Set End Property End Class