Declare events in VB classes

Source: Internet
Author: User
Tags argumentlist
[Materials]
1. Event statement

Define User-Defined events.

Syntax

[Public] event procedurename [(Arglist)]

The event statement contains the following parts:

Partial description
Public. Specify that the event is visible throughout the project. By default, the events type is public. It should be noted that the event can only be declared in Module .
Procedurename is required. The name of the event. Follow the standard variable naming conventions.


Arglist Parameters Each part of the syntax and syntax is as follows:

[Byval | byref] varname [()] [as type]

Partial description
Byval is optional. This parameter is passed by value.
Byref is optional. This parameter is passed by address. Byref is the default setting of Visual Basic.
Varname is required. Indicates the name of the parameter variable to be passed to the process. Standard Variable naming conventions.
Type is optional. The data type of the parameter passed to the process. It can be byte, Boolean, integer, long, currency, single, double, decimal ( Currently Not 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 the event declaration appears in the standard module, a syntax error occurs. Events with returned values cannot be declared. In the following code segment, we provide typical events for declaring and Generating Events:

'Declare an event at the module level of the class module

Event logoncompleted (username as string)

Sub
Raiseevent logoncompleted ("antoinejan ")
End sub

Note that the parameters of an event can be declared like the parameters of the Declaration process, but the events cannot contain named parameters, optional parameters, or paramarray parameters. The event has no return value.

2. raiseevent statement

An event that is declared at the module level in a class, form, or document.

Syntax

Raiseevent eventname [(argumentlist)]

The required eventname is the name of an event declared in the module and complies with the Basic variable naming conventions.

The syntax of the raiseevent statement is as follows:

Partial description
Eventname is required. The name of the event.
Argumentlist is optional. A list of variables, arrays, or expressions separated by commas. Argumentlist must be enclosed in parentheses. If no parameter exists, parentheses must be ignored.


Description

If the event is not declared in the module where the event is triggered, an error occurs. The following Program This section describes the declaration of an event and the process of triggering the event.

'Declare an event at the module level of the class module
Event logoncompleted (username as string)

Sub
.
Raiseevent logoncompleted ("antoinejan ")
End sub

If there is no parameter for this event in raiseevent, including empty parentheses, the call to this event will cause an error. You cannot use raiseevent to trigger events that are not explicitly defined in the module. For example, if a form has a click event, you cannot use raiseevent to trigger the Click Event of the form. If a click event is declared in the form module, it overwrites the form's own click event. You can still use the normal syntax to call the event to call the Click Event of the form, but you cannot use the raiseevent statement.

Events are triggered based on the order in which connections are established. Because the event can have the byref parameter, the connected process may later receive the parameter that has been changed by an earlier event handler.

3. writeproperties event

This event occurs when the instance of the object is saved. The Event Notification object needs to save the object State so that it can be restored in the future. In most cases, the object state only includes attribute values.

Syntax

Sub object_writeproperties (PB as propertybag)

The writeproperties event syntax includes the following parts:

Partial description
Object object expression. Its value is "applied to" the object in the list.
Objects of the propertybag type to be written by Pb.

Description

When the writeproperties event occurs, the object creator calls the writeproperty method of the propertybag object for each data to be saved to save the object state.

Note that the Pb property package may be different from the Pb passed to the recent readproperties event.

During the lifetime of the object instance, The writeproperties event may occur multiple times.



5. to trigger an event defined in the class module, you must add the withevent statement in the Declaration!
Otherwise, you cannot write an event processing module for the event!
Just like this!
Dim withevent A as OBJ
Set a = new OBJ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.