In this section, let's practice it. First create a COM object, then use it, and finally think of improvements.
First do the following two steps:
Run Visual Basic; select the "StandardEXE" project type; since COM objects are based on classes, classes are actually packages, just like the code in the module. so:
Select "PRoject"->"AddClassModule"; when the corresponding dialog box appears, select "'ClassModule", and then click the "Open" button.
In this way, a form is displayed on the desktop, as well as Class1 contained in Project1.
Next, change the class name of the empty class:
In the properties window of the class, change the Name property of the class to CDog.
It should be noted that for the sake of distinction, each object name has a corresponding prefix, for example, the TextBox object is preceded by "txt", the Form is preceded by "frm", and the class can be preceded by the uppercase letter "C" or the lowercase letter "cls" ", but the former is used here.
Let's add some code to test it:
In the CDog class general declaration section, add the declaration of the variable:
Then, open Form1;
Add a command button to the form;
Open the code window and add the following code to the command button:
Let’s explain the meaning of the above code:
This line of statement is used to notify Visual Basic to set a bit space for the CDog object. However, the object cannot be used at this time and must wait until the next statement:
It is used to create instances of CDog. This means that the previously empty MyDog template becomes a CDog object that can now be used.
The first line of the above code is used to set the Name variable of MyDog, and the second line of statements is used to display the contents of the variable in the message dialog box. at last:
Used to simply empty the MyDog object.
Press F5 to run and test.
How about it? But at the same time, we may not help but ask, what is the difference between standard modules and class modules? Let's look at the following example again:
Change the command button code to:
Different from the previous code, the code here actually defines two objects MyDog and MyDog2, which are two independent objects based on CDog.
Press F5 to run and test.
What was the result? Do two dialog boxes appear this time? One says "BillyMoore" and the other says "SadieMoore".
Except for Name, each object defined above does not have any actual attributes, so the following process will be added:
Open the previous Class1;
Declare the following public variables:
Open the previous Form1;
Change the command button code to:
The code is similar to the previous one, except that the Age variable is used here.
Press F5 to run and test.
Two message dialog boxes should appear showing the contents of name and age.
Now try setting the age value of one of the objects to 1,000 or 30,000. See how it turns out? The program still runs normally because the maximum value of the defined integer variable can reach 32,767, but the actual dog (Dog) is not 30,000 years old.
So, how should this situation be handled?
->