Sometimes, in order to better deal with more properties. For example, if you have four different customer bases: Group, large, smaller, and new customer types, or three different search methods in the class: floppy disk, hard disk, and network. So, can you choose one from the list of options, instead of setting related properties with numbers or text that cannot be understood?
I think the answer is sure. Because this treatment is called "enumeration".
Open the project in the previous section, let us add some code.
Add the following code to the CDOG class:
Public enum coattype bigandshaggy = 1 shortcrewcut = 2 PoodleStyleafro = 3 unknown = 4 End enum
Keywords "enum" is used to define enumerations, in other words, it is a list of possible options. Each option has a corresponding number, that is, BigandShaggy represents 1, ShortcrewCut is equal to 2, and so on.
It should be noted that when an enumeration related information is added to the database, its corresponding value is very useful. Since "BigAndshaggy" actually represents a value 1, it can be inserted into the value field of the database. This means that you can easily use the string to maintain the database.
So let's create a list of Dog's Coat type and define an attribute to add these types in the CDOG class.
Declare the following variables in the class:
Private udtcoat as coattype
This defined private variable is used to save the upcoming Coat type properties, notice that the UDTCOAT variable is neither a string nor integer, but our own defined enumeration type CoatType.
When class CDOG is open, select the "Add Procedure" command in the "Tools" menu to pop up the corresponding dialog box;
Type Coat in the Name Editing box;
Check the "Property" option button, then click [OK].
The system automatically produces the following code framework:
Public Property Get Coat () AS VARIANT End Property Public Property Let Coat (Byval VNewValue As Variant) End Property
But we need it is not this framework. In the code, the "Variant" variable type can receive and process any type of data. In the CDOG class we define, the last attribute is AGE, which can only be accepted. But now you need a property to receive the data type in the CoatType list, so you need to make the following modifications:
All "Variant" will be changed to "Coattype" in the code.
Then, add some code that actually handles attributes.
Add the following code during the Get attribute:
Coat = udtcoat
Add the following code during the list of attributes:
UDTCOAT = VNewValue
Switch to Form1;
Change the code of the Command button to:
DIM MyDog as cdogset mydog = new cdogmydog.name = "billy"
Type now: mydog.coat =
The miracle appeared, when you knock down the "=" button, there is a list containing possible options, from which we can choose one. Type of completion code: mydog.coat = shortcrewcut
Next, we will get the value of the COAT attribute. If you simply display the property value in the Message dialog now, you can only return the value of the selection. For example, if shortcrewcut is selected, its property must return 2. Don't believe, you can try it!
But use another method here, it is used to judge Coat with if-the meaning
Add the following code to the COMMAND button existing code:
If MyDog.Coat = BigAndShaggy Then MsgBox "You have a big, bouncy, bushy pup!" ElseIf MyDog.Coat = PoodleStyleAfro Then MsgBox "Your pooch is pretty, petit and pooch-like!" ElseIf MyDog.Coat = ShortCrewCut Then MsgBox " Your Dog Is Full of OMPH, OMPH AND More OMPH! "Elseif MyDog.coat = Unknown Ten Msgbox" i Have No IDEA About Your Dog. I don't think "& _" You do Either! "Endiff
The code here just simply determines the COAT attribute value and displays the corresponding message dialog. Of course, you can also use the "SELECT CASE" statement here.
Finally, we add the last statement to release computer memory:
Add the following code to the COMMAND button existing code:
Set mydog = Nothing
Press F5 to run the program and click the Command button to test it.
What is the result?