Event statement
Define user-defined events.
Grammar
[Public] Event procedurename [(arglist)]
The Event statement contains the following sections:
Section description
Public is optional. Specifies that the Event is visible throughout the project. By default, the Events type is public. It should be noted that events can only be generated in the declared module.
ProcedureName is required. The name of the event, followed by a standard variable naming convention.
The syntax of the arglist parameter and the various parts of the syntax are as follows:
[ByVal | BYREF] varname[()] [as type]
Section description
ByVal is optional. Indicates that the parameter is passed by value.
ByRef Optional. Indicates that the parameter was passed by address. BYREF is the default setting for Visual Basic.
VarName is required. Represents the name of the parameter variable to pass to the procedure, followed by a standard variable naming convention.
The type is optional. Refers to the data type of the parameter passed to the procedure, which can be a Byte, Boolean, Integer, Long, Currency, single, Double, Decimal (not currently supported), date, String (variable length only), Object, Variant, user-defined type, or object type.
Description
After the event is declared, you can use the RaiseEvent statement to generate the event. If an event declaration occurs in a standard module, a syntax error is generated. You cannot declare an event with a return value. In the following code snippet, a typical event that declares an event and produces an event is given:
' Declare an event in the module level of the class module
Event logoncompleted (UserName as String)
Sub
RaiseEvent logoncompleted ("Antoinejan")
End Sub
Note You can declare the parameters of an event just like the arguments for a procedure, but the following are different: events cannot have named arguments, Optional parameters, or ParamArray parameters. The event does not return a value.
=======================================================================================
Applying attribute procedures
Visual Basic provides the three property procedures listed in the following table.
Process Purpose
Property Get Returns the value of an attribute
Properties let set the value of the property
The property set sets the value of the object property (that is, it contains an object reference).
As can be seen from the table above, each property procedure has its own special function. A typical property consists of a pair of property procedures: A property Get retrieves the value of an attribute, and a property Set assigns a value to an attribute.
These effects may overlap in some cases. There are two types of property procedures that are assigned because Visual Basic has a special syntax when assigning an object reference to an object variable:
Dim WDG as Widget
Set WDG = New Widget
The rule is simple: if there is a set statement, Visual Basic calls the property Set, and if not, calls Property Let.
Tip return to the classic Basics syntax, in order to properly use the property Let and property Set to keep clean and tidy, you should type Let X = 4 (Until today Visual Basic also supports the syntax) instead of x = 4. Visual Basic always determines the invocation of a property procedure based on the type of assignment-for Let x = 4, call properties let, and the property set for Set C1 = New Class1 (object properties).
Details "Object use" in "Programming fundamentals" interprets the use of Set statements for object variables.
Read/write properties
The following encodings show typical read and write properties:
' Property value of the private store.
Private Mintnumberofteeth as Integer
Public Property Get Numberofteeth () as Integer
Numberofteeth = Mintnumberofteeth
End Property
Public Property Let Numberofteeth (ByVal newvalue _
As Integer)
' (Omit the Validate attribute value code.) )
Mintnumberofteeth = newvalue
End Property
A private variable name that stores the property value, consisting of a scope prefix (m), type prefix (int), and name (Numberofteeth), where the scope prefix (m) indicates that the variable is at the module level. Use the same name as the property to indicate that the variable and the property are related.
There is no doubt that, as in previous examples, property procedure names that contain read and write properties must be the same.
Note that property procedures are public by default, so if you omit the public keyword, the property process is still common. If you want to make the property private (only accessible within the object), you must declare it with the private keyword. Even if it is not necessary, it is best to use the Public keyword because it makes the intent clear.
Run and Play Property procedures
It is useful to run some property procedure code in one step. Using the Project menu, open a new standard EXE project and add a class module. Copy the code from the Numberofteeth property to Class1, as shown above.
Switch to Form1 and add the following code to the Load event:
Private Sub Form_Load ()
Dim C1 as Class1
Set C1 = New Class1
' Assigns the new attribute value.
C1. Numberofteeth = 42
' Display attribute values.
MsgBox C1. Numberofteeth
End Sub
Press the F8 key to run the code in one step. Note that when the property is assigned a value, the run jumps to the property let, and when the attribute value is retrieved, the run jumps to properties get. You will find it useful to repeat this exercise for other combinations of attribute processes.
The parameters of the property procedure pair must match
The property procedure examples that have been seen so far are simple, as is the case with the property process for most properties. However, property procedures can have multiple parameters-even optional parameters. The most useful of the multiple parameters is the array, which is discussed below.
When multiple parameters are used, the parameters of the property procedure pair must match. The following table shows the requirements for parameters in a property procedure declaration.
Procedure declaration Syntax
Property Get Property Get PropertyName (1,..., N) as type
Property Let PropertyName (1,..., N, n+1)
Property Set Property Set PropertyName (1,..., N, n+1)
In the property process, from the first argument to the second-to-last argument (1,..., N), you must have the same name and data type as the procedure. As with other procedure types, the required parameters in the list should be preceded by the first optional parameter.
It may be noted that the declaration of the property Get procedure is one less argument than the related Property let or property set. The declaration of the data type of the property Get procedure must be the same as the data type of the property let or the last parameter of the property Set (n+1).
For example, a property-let declaration of an attribute that resembles a two-dimensional array:
Public Property Let things (ByVal X as Integer, _
ByVal Y as Integer, ByVal Thing as Variant)
' (omit the code that assigns values to the array element.) )
End Property
The parameter declaration of property Get must be the same as the parameter name and data type of the property Let procedure:
Public Property Get Things (ByVal X as Integer, _
ByVal Y as Integer) as Variant
' (omit the code from the array to take the value.) )
End Property
The data type of the last parameter in the property Set declaration, either an object type or a Variant type.
Parameter matching
Figure 9.8 shows the rationale for these parameter matching rules. It shows how Visual Basic matches the assignment statement part to the Property Let parameter.
Figure 9.8 Call of the Property Let procedure
Property procedures with multiple parameters are most commonly used to create an array of attributes.
Read-only properties
To create a read-only attribute, simply omit Property let or (for object properties) property Set.
Object Properties
To create a read-write object property, you should use the Properties Get and attribute Set such as:
Private Mwdgwidget as Widget
Public Property Get widget () as Widget
' Set statement is used to return an object reference.
Set Widget = Mwdgwidget
End Property
Public Property Set Widget (ByVal newwidget as widget)
Set Mwdgwidget = Newwidget
End Property
Variants Property
The read-write property of the variants data type is the most complex. As shown below, use three types of Property procedure:
Private mvntanything as Variant
Public Property Get Anything () as Variant
The ' Set statement is used only for any property that contains an object reference.
If IsObject (mvntanything) Then
Set anything = mvntanything
Else
anything = mvntanything
End If
End Property
Public Property Let anything (ByVal NewValue as Variant)
' (Omit the validation code.) )
mvntanything = Newwidget
End Property
Public Property Set Anything (ByVal newvalue as Variant)
' (Omit the validation code.) )
Set mvntanything = Newwidget
End Property
In the right environment, property Set and Property Let are always used normally. However, the following two scenarios must be considered when using property Get:
strsomestring = objvar1. Anything
Set objvar2 = objvar1. Anything
In the first case, the Anything property contains a string, which is assigned to a string variable. In the second case, the Anything property contains the object reference, which is assigned to the object variable.
By using the IsObject function to test the private variants variable before the return value, you can program the property Get to handle both cases.
Naturally, when a property contains an object reference, an error occurs if the first encoding is called, but this is not a property Get problem, but rather a problem with the variants attribute.
Write property only once
There are several possible combinations of property procedures. All combinations are valid, but some are relatively less common, such as write-only properties (only property is let, without the attribute Get). Some depend on additional factors, rather than on the combination of attribute procedure types.
For example, as described in the object model later in this chapter, when you organize an object in a program by creating an object model, you might want to have the object reference the object that contains it. Use the Parent property to do so.
When an object is created, the Parent property should be set, but thereafter you may want to prevent it from being intentionally or unintentionally changing. The following example shows how the account object uses the Parent property. This property points to the Department object that contains the account number.
' Parent property to be stored with private data
Private Mdeptparent as Department
Property Get Parent () as Department
' Use the Set statement to reference the object.
Set Parent = mdeptparent
End Property
' Property value can only be set once.
Public Property Set Parent (ByVal newparent _
As Department)
If Deptparent is Nothing Then
' Assign an initial value.
Set mdeptparent = newparent
Else
Err.Raise Number:=vbobjecterror + 32144, _
Description:= "Parent property is Read-only"
End If
End Property
Property Get is used to return a reference to the parent object when accessing the parent object of the account object, for example, by obtaining the department name through code STRX = AcctNew.Parent.Name.
In this example, the property set is programmed so that the Parent attribute can only be set once. For example, when a Department object creates a new account, you can execute code set acctnew.parent = Me to set the property. Thereafter, the property is read-only.
For more information because the form is a class in Visual Basic, you can add user properties to the form. See the "Customizing Form Classes" section earlier in this chapter.
======================================================================
Property Get Statement
Declares the name of the property procedure, the parameters, and the code that makes up its body, which gets the value of an attribute.
Grammar
[Public | Private | Friend] [Static] Property Get name [(arglist)] [as type]
[Statements]
[NAME = expression]
[Exit Property]
[Statements]
[NAME = expression]
End Property
The syntax of the property Get statement contains the following sections:
Section description
Public is optional. All other procedures that represent all modules have access to the property Get procedure. If used in a module that contains Option Private, the procedure is not available outside of the project.
Private Optional. Indicates that only other procedures that contain the module that contains its declaration can access the property Get procedure.
Friend is optional. Can only be used in class modules. Indicates that the property Get process is visible throughout the project, but is invisible to the controller of the object instance.
Static Optional. Represents the value of a local variable that retains the property Get procedure between calls. The Static property has no effect on declaring a variable outside of the properties Get procedure, even if the variables are used in the procedure.
Name is required. The name of the property Get procedure, followed by a standard variable naming convention, but not with the same name as a property let or a property Set procedure in the same module.
ArgList is optional. Represents a list of variables to pass to the Property Get procedure when called. Multiple variables are separated by commas. The name and data type of each parameter in the property Get procedure must match the parameters in the corresponding Property Let procedure (if present).
The type is optional. The data type of the return value of the Property Get procedure, which can be a Byte, Boolean, Integer, Long, Currency, single, Double, Decimal (not currently supported), date, String (except fixed length), Object, Variant, or any user-defined type. An array of any type cannot be a return value, but a Variant containing an array can be a return value.
The return value type of the property Get procedure must be the same as the data type of the last (and sometimes the only) parameter of the corresponding Property Let procedure (if any), which assigns the value of its right expression to the attribute.
Statements is optional. Any set of statements executed in the property Get procedure body.
expression is optional. Property value returned by the procedure defined by the Properties Get statement.
The syntax of the arglist parameter and the various parts of the syntax are as follows:
[Optional] [ByVal | BYREF] [ParamArray] varname[()] [as type] [= defaultvalue]
Section description
Optional is optional. Indicates that the parameter is not required. If this option is used, subsequent parameters in arglist are optional and must be declared using the Optional keyword.
ByVal is optional. Indicates that the parameter is passed by value.
ByRef Optional. Indicates that the parameter is passed by address. BYREF is the default option for Visual Basic.
ParamArray is optional. Used only for the last parameter of arglist, indicating that the last parameter is a Variant element of the Optional array. Use the ParamArray keyword to provide any number of arguments. The ParamArray keyword cannot be used with ByVal, BYREF, or Optional.
VarName is required. The name of the variable that represents the parameter, followed by the standard variable naming convention.
The type is optional. The data type of the parameter passed to the procedure, which can be a Byte, Boolean, Integer, Long, Currency, single, Double, Decimal (not currently supported), date, String (variable length only), Object, or Variant. If the parameter is not Optional, it can also be a user-defined type, or an object type.
DefaultValue is optional. Any constant or constant expression. Valid only when the Optional parameter is in the. If the type is Object, the explicit default value can only be nothing.
Description
If you do not explicitly specify with Public,private or Friend, the property procedure defaults to public. If Static is not used, the value of the local variable is not preserved after the call. The Friend keyword can only be used in class modules. A Friend process can be accessed by the process of any module in the project. A friend procedure does not appear in the type library of its parent class, and the friend process cannot be late-bound.
All executable code must be part of a process. You cannot define a property Get procedure in another property, Sub, or Function procedure.
The Exit Property statement causes execution to exit immediately from a property Get procedure. The program then starts execution from the next statement that invokes the property Get procedure. You can have the Exit property statement anywhere in the Get process.
The Property Get procedure is similar to the Sub and Property Let procedure in that the Property Get procedure is an independent process that can get parameters, execute a series of statements, and change the values of its parameters, unlike sub and Property Let procedures: When you want to return the value of an attribute, you can use the property Get procedure on the right side of an expression, just as you would with a Function or property name.
================================================================
Property Let statement
Declares the name, parameter, and code of the property's let procedure, which assigns a value to an attribute.
Grammar
[Public | Private | Friend] [Static] Property Let name ([arglist,] value)
[Statements]
[Exit Property]
[Statements]
End Property
The syntax for the property Let statement contains the following sections:
Section description
Public is optional. Indicates that all other procedures for all modules can access the Property Let procedure. If used in a module that contains Option Private, this process is not available outside of the project.
Private Optional. Indicates that the property Let procedure can be accessed only in other procedures that contain the module for which it is declared.
Friend is optional. Can only be used in class modules. Indicates that the property-let procedure is visible throughout the project, but not for the controller of the object instance.
Static Optional. Represents the value of a local variable that will preserve the property-let procedure between calls. The Static property has no effect on variables declared outside of the properties let procedure, even if the variables are used in the procedure.
Name is required. The name of the Property Let procedure, followed by a standard variable naming convention, but not with the same name as a property Get or property Set procedure in the same module.
ArgList is optional. Represents a list of variables that are passed to the property-let procedure when called. Multiple variables are separated by commas. The name and data type of each parameter in the Property Let procedure must be the same as the corresponding parameter in the property Get procedure.
Value is required. Refers to a variable that is used to assign a value to a property. When the procedure is called, this parameter appears to the right of the invocation expression. The data type of value must be the same as the return value type of the corresponding property Get procedure.
Statements is optional. Any statement group that is executed during the property Let procedure.
The various parts of the syntax and syntax of the arglist parameter are as follows:
[Optional] [ByVal | BYREF] [ParamArray] varname[()] [as type] [= defaultvalue]
Section description
Optional is optional. Indicates that the parameter is not required. If this option is used, subsequent parameters in arglist must be optional and must be declared with the Optional keyword. Note that the right side of a property let expression is not possible for Optional.
ByVal is optional. Indicates that the parameter is passed by value.
ByRef Optional. Indicates that the parameter is passed by address. BYREF is the default option for Visual Basic.
ParamArray is optional. Used only for the last parameter of arglist, indicating that the last parameter is a Variant element of the Optional array. Use the ParamArray keyword to provide any number of arguments. The ParamArray keyword cannot be used with ByVal, BYREF, or Optional.
VarName is required. Represents the name of a parameter variable, followed by a standard variable naming convention.
The type is optional. The data type of the parameter passed to the procedure, which can be a Byte, Boolean, Integer, Long, Currency, single, Double, Decimal (not currently supported), date, String (variable length only), Object, or Variant. If the parameter is not Optional, it can also be a user-defined type, or an object type.
DefaultValue is optional. Any constant or constant expression. Valid only when the Optional parameter is in the. If the type is Object, the explicit default value can only be nothing.
Note Each Property Let statement must define at least one parameter for the procedure that it defines. When the procedure defined by this property Let statement is called, the argument (if there are multiple arguments, the last one) contains the actual value that will be assigned to the attribute. This parameter is the value in the preceding syntax.
Description
If you do not explicitly specify by using public, Private, or Friend, the property procedure is common by default. If Static is not used, the value of the local variable is not preserved after the call. The Friend keyword can only be used in class modules. However, the Friend process can be accessed by any module in the engineering process. A friend procedure does not appear in the type library of its parent class, and the friend process cannot be late-bound.
All executable code must be part of a process. You cannot define a property-let procedure in another property, Sub, or Function procedure.
The Exit Property statement causes execution to exit immediately from a property-let procedure. The program then executes from the next statement that invokes the property's let procedure. You can have an Exit property statement anywhere in the process.
A property-let procedure is similar to the Function and property Get procedure in that a property-let procedure is an independent process that takes parameters, executes a series of statements, and alters its parameter values. Unlike the Function and property Get procedures, both procedures have a return value, and the property Let procedure can be used only on the left side of an attribute expression or let statement.
====================================================================
Property Set Statement
Declares the name of the property procedure, the parameters, and the code that makes up its body, which sets an object reference.
Grammar
[Public | Private | Friend] [Static] Property Set name ([arglist,] reference)
[Statements]
[Exit Property]
[Statements]
End Property
The syntax of the property Set statement contains the following sections:
Section description
Optional is optional. Indicates that the caller can supply or not supply the parameter.
Public is optional. This property Set procedure is accessible to all other processes that represent all modules. If used in a module that contains Option Private, this process is not available outside of the project.
Private Optional. Indicates that the property Set procedure can be accessed only by other procedures that contain the module for which it is declared.
Friend is optional. Can only be used in class modules. Indicates that the property Set procedure is visible throughout the project, but is invisible to the controller of the object instance.
Static Optional. Represents the value of a local variable that retains the property Set procedure between calls. The Static property has no effect on variables declared outside of the properties Set, even if the variables are used in the procedure.
Name is required. The name of the property Set procedure, followed by a standard variable naming convention, but not with the same name as a property Get or Property Let procedure in the same module.
ArgList is optional. A list of variables that represent the arguments to pass to the property Set procedure when called. For multiple variables, separate them with commas.
Reference is required. The object references the variable that contains the object reference that is used on the right side of the assignment.
Statements is optional. Any set of statements executed in the property procedure body.
The syntax of the arglist parameter and the various parts of the syntax are as follows:
[Optional] [ByVal | BYREF] [ParamArray] varname[()] [as type] [= defaultvalue]
Section description
Optional is optional. Indicates that the parameter is not required. If this option is used, subsequent parameters in arglist must be optional and must be declared with the Optional keyword. Note: The right side of the property Set expression cannot be Optional.
ByVal is optional. Indicates that the parameter is passed by value.
ByRef Optional. Indicates that the parameter is passed by address. BYREF is the default option for Visual Basic.
ParamArray is optional. Used only for the last parameter of arglist, indicating that the last parameter is a Variant element of the Optional array. Use the ParamArray keyword to provide any number of arguments. The ParamArray keyword cannot be used with ByVal, BYREF, or Optional.
VarName is required. The name of the variable that represents the parameter, followed by the standard variable naming convention.
The type is optional. The data type of the parameter passed to the procedure, which can be a Byte, Boolean, Integer, Long, Currency, single, Double, Decimal (not currently supported), date, String (variable length only), Object, or Variant. If the parameter is not Optional, it can also be a user-defined type, or an object type.
DefaultValue is optional. Any constant or constant expression. Valid only when the Optional parameter is in the. If the type is Object, the explicit default value can only be nothing.
Note Each property Set statement must define at least one parameter for the procedure that it defines. When the procedure defined by the property Set statement is called, this parameter (if there are multiple arguments, the last one) contains the actual object reference that will be assigned to the attribute. This parameter is the reference in the aforementioned syntax. It can't be Optional.
Description
If you do not explicitly specify with public, Private, or Friend, the property procedure is common by default. If Static is not used, the value of the local variable is not preserved after the call. The Friend keyword can only be used in class modules. However, the Friend process can be accessed by any module in the engineering process. A friend procedure does not appear in the type library of its parent class, and the friend process cannot be late-bound.
All executable code must be part of a process. You cannot define a property Set procedure in another property, Sub, or Function procedure.
The Exit Property statement causes execution to exit immediately from a property Set procedure. The program then executes from the next statement that invokes the property Set procedure. You can have an Exit property statement anywhere in the Set procedure.
Property Set procedures are similar to Function and property Get processes in that they are an independent process that can get parameters, execute a series of statements, and change the values of their parameters. Unlike the Function and property Get procedures, both procedures have return values, and the property Set procedure can only be used on the left side of an object reference assignment (Set statement).
The property and event of VB