In this section, let's practice. First create a COM object, then use it, and finally improve it.
First, the following two steps:
Run Visual Basic; select the "Standard Exe" project type; because the COM object is based on classes, the class is actually a package, just like the code in the module. and so:
Select "Project" -> "add class module"; select "'Class Module" when the corresponding dialog appears, and then click the "Open" button.
This way, display a form on the desktop, and Class1 included in the project Project1.
Change the empty class class name:
In the properties window of the class, change the name of the Name property to CDOG.
It should be noted that in order to distinguish, there is a corresponding prefix in front of each object name. For example, the Text Box object is "TXT", and Form is "FRM", and the class can capitalize "C" or lowercase letters before class. " CLS, but use the former here.
Below we add some code to test:
In the Universal Declaration section of the CDOG class, the declaration of add variables:
Public Name As String
Then, open FORM1;
Add a command button in the form;
Open the code window, add the following code for the command button:
Dim mydog as cdog set mydog = new cdog mydog.name = "billy moore" msgbox mydog.name set mydog = Nothing
Let's explain the meaning of the above code:
DIM MYDOG As CDOG
The row statement is used to inform Visual Basic set a bit space for the CDOG object, but it is not possible to use this object, and must wait until the following statement:
Set mydog = new cdog
It is used to create an instance of CDOG. This means that the front empty MYDOG template becomes the CDOG object that can now be used.
MyDog.name = "billy moore" msgbox mydog.name
The first line of the above code is used to set the NAME variable of MyDog, and the second row statement is used to display the content of the variable in the message dialog. At last:
Set mydog = Nothing
It is used to simply blank myDog objects.
Press F5 to run and test it.
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 take a look at the example below:
Change the code of the command button:
Dim MyDog As CDog Set MyDog = New CDog Dim MyDog2 As CDog Set MyDog2 = New CDog MyDog.Name = "Billy Moore" MsgBox MyDog.Name MyDog2.Name = "Sadie Moore" MsgBox MyDog2.Name Set MyDog = Nothing Set MyDog2 = Nothing
Unlike the front code, the code here is actually defining two objects MYDOG and MyDog2, which is two objects based on CDOG.
Press F5 to run and test it.
What is the result? Is this time no two dialogs appeared? A display "Billy Moore" and another display "Sadie Moore". In addition to the NAME, there is no actual attribute in each object described above, so the following procedure is added:
Open the front Class1;
Declare the public variable below:
Public Age As Integer
Open the front of Form1;
Change the code of the command button:
DIM MyDog as cdog set mydog = new cdog Dim mydog2 as cdog set mydog2 = new cdog mydog.name = "billy moore" mydog.age = 4 msgbox mydog.name & "is" & mydog.age & "years" mydog2. Name = "sadie moore" mydog2.age = 7 msgbox mydog2.name & "is" & mydog2.age & "years" set mydog = nothing set mydog2 = nothing set mydog2 = Nothing
These codes are similar in front, but only the AGE variable is used here.
Press F5 to run and test it.
Two messages dialogs that appear in the contents of the Name and AGEs should appear.
Now try to set the Age value of one of the objects to 1,000 or 30,000. Look at the results? The program is normal to operate, because the defined integer variable is up to 32,767, but the actual dog (DOG) will not have 30,000 years old.
So what should this happen?