"Interpretation and Application of VBA Classes" tutorial [10165646] is the fifth set of tutorials I have launched, and it is now the first version of the revision. This set of tutorials is positioned at the most advanced level, and it is a tutorial after learning elementary and intermediate levels.
Classes are very abstract and have more research value. As we learn and apply VBA in depth, it is necessary to understand these abstract theoretical knowledge. Objects, Classes, Procedures, Methods, Properties, Events, Interfaces, How Interfaces Are Implemented, and so on. By mastering these theories, you will not only have a deep understanding of the essence of the parasitic language of VBA, but also many things in the natural world. At present, this set of tutorial program files has passed the 32-bit and 64-bit office system tests.
This set of tutorials consists of two volumes, 84 lectures, and the revised tutorial content will be introduced to you in the future. Today's content is: Interpretation and Application of Classes in VBA Lecture 9: Instantiating Object Class Variables with the WithEvents Keyword Declaration
Think about it, how many evils have you created that have not yet been repented and purified? How difficult is it to die without following the bad tastes? Judging by the current situation of most people, there is no certainty that they will not fall into the three evil realms. However, as long as you rely on Amitabha Buddha wholeheartedly, you can be reborn directly into the Pure Land after the end of this life, completely free from the evil realms, and even transcend the sea of suffering of birth and death forever.
In the previous lectures, we first defined the class, named the name of the class, and defined the event of the class, in which we know that the event refers to a call raised by the class. is the response of the object. To be more precise with this definition, an event is a call raised by a class; is the response of the object after the class is instantiated. It would be easier to understand if we changed the following object to an instantiated object. First name a class, then declare the events that the class responds to, but you must instantiate the class before you can use it.
The instantiated class object that raises the event is calledEvent sourcesIn order to handle events raised by the event source, you can declare an instantiated class object variable with the withevents keyword. Let's understand the meaning of this sentence again: in practical applications, we are facing objects, when a class object appears as a variable, it can respond to an event, and when declaring such an object variable, we use the declaration statement withevents.
1) The withevents variable cannot be a derived object variable. That is, it cannot be declared as an object (the class name must be specified when declaring the variable).
eg: Wrong way to write dim withevents a as object
Correct way to write it: dim withevents a as text
2) The withevents variable cannot be declared as new, that is, the withevents keyword and the new keyword cannot be used at the same time. If you define it as follows, you will get an error: as new text
3) The withevents variable cannot be declared in the standard module. It can only be declared in class modules, form modules, and other modules that define classes.
4) Although withevents is the keyword of dim, public, and private statements, you can't use the withevents keyword to declare object variables in the process, and you can only use it in general.
5) You can only declare a single variable with withevents, not an array.
Example: Today we will continue to explain the above example, in the previous lecture we defined the mytime class, in this class, we declared two events as follows, one is updatetime and the other is dabiao.
option explicit
public event updatetime(byval mynow as double)
public event dabiao()
*Screenshot:
These two events are mytime-like events that are to be instantiated to respond to after the mytime is formed. So what is the object formed by instantiating mytime?
I made a declaration of withevents in the form module:
private withevents mtext as mytime
That is to say, in the form, you need to instantiate the mytime formation class object as mtext, which is implemented in the construction of the form:
option explicit
private withevents mtext as mytime
private sub commandbutton1_click()
textbox1.text = "Start Timer:"
textbox2.text = "0"
mtext.timertask (9)
end sub
private sub commandbutton2_click()
endend sub
private sub mtext_dabiao()
textbox1.text = "The standard has been met"
doevents
end sub
private sub mtext_updatetime(byval mynow as double)
textbox2.text = str(format(mynow, "0"))
doevents
end sub
private sub userform_initialize()
textbox1.text = ""
textbox2.text = ""
set mtext = new mytime
end sub
private sub userform_terminate()
endend sub
As shown in the following form**screenshot:
In the form constructor, set mtext = new mytime is used to instantiate the soul of the class as a physical object mtext.
In this way, we can use this object to respond to the event updatetime, dabiao, and let's take a look at the screenshot of the event prompted by the window
Two events are prompted and can be exploited.
At this point, we have gradually understood the relevant knowledge of classes, which is more abstract, but not very difficult to understand, and will use these knowledge points in the future explanation to achieve our specific application.
For this lecture, please refer to the program file: VBA-CLASS(1-28).xlsm
My more than 20 years of practical experience in VBA is condensed in the following tutorials: