Attribute operations are very similar to public variables, but attributes have more controls.
Common properties typically include "GET" and "Let (setting)". These two operations can specify a major attribute, just like the following code snippet:
PRIVATE INTAGE () AS INTEGER AGE () AGE (BYVAL VNEWVALUE AGER) INTAGER = VNEWVALUE END Property
The working mode of the above code is imaging the operation of the AGE variable. When operating variables like the following statement,
Mydog.age = 4
It is better than running the list attribute and assigns 4 to VNewValue. When operating variables like the following statement,
Msgbox mydog.age
It is obtained attribute, which is better than running the get attribute and returns the corresponding value by INTAGE. Therefore, we can think so:
The so-called Get property is to get a certain value after running;
The so-called let attribute is to make a certain value after running;
As so far, we only explain that attribute work mode is very similar to the standard variable, and there is no more control over the attribute. So, discuss it below.
Open the project in the previous section, modify the CDOG class:
Remove the AGE variable from the CDOG class;
Add the following code:
FACE = "Courier" SIZE = 2> Private intAge As Integer Public Property Get Age () As Integer Age = intAge End Property Public Property Let Age (ByVal vNewValue As Integer) If vNewValue <= 50 Then intAge = vNewValue End If End Property
Compared with the previous code, this is just a slight modification for the LET property code. The following is a small test, the imaginary user tries to make:
Mydog.age = 30
That is to run the Let property, so that VNewValue is equal to 30. In the code, VNEWVALUE is also detected whether it is less than or equal to 50. Obviously, 30 is in line with the requirements, so the INTAGE value in the example is equal to 30. But if more than 50, what does not happen, the property exits without any assignment. Of course, we can also give this corresponding error code or display a prompt dialog.
Switch to the code window behind FORM1;
In the first line statement set at the AGE property code, click F9;
Mydog.age = 4
In the first line statement at which the AGE property code is obtained, click F9;
Msgbox mydog.name & "is" & mydog.age & "years old"
Let us test now:
Press F5 to run the program;
Click the Command button;
The code should be interrupted on the code line of the breakpoint added by F9.
When the code is interrupted, press F8 to run and observe the result;
I understand how they work now? Note How does "get" and "let" running at the age property?