Sometimes, more attributes are needed to better handle a class. For example, say you have four different customer groups: corporate, larger, smaller, and new customer types, or a class for search with three different search methods: floppy, hard drive, and network. So, wouldn't it be better to choose one from a list of options instead of setting the relevant property with numbers or text that you can't understand?
I think the answer is yes. Because this kind of processing is called "enumeration".
Open the project from the previous section and let's add some code.
Add the following code to the CDog class:
The keyword "Enum" is used to define an enumeration, in other words, it is a list of possible options. Each option has a corresponding number, meaning BigAndShaggy equals 1, ShortCrewCut equals 2, and so on.
It should be noted that when adding information about enumeration items to the database, its corresponding values are very useful. Since "BigAndShaggy" actually represents the value 1, it can be directly inserted into the numerical field of the database. This means that you can easily use strings to maintain the database.
So, let's create a list of Dog's Coat types and define another attribute to add these types to the CDog class.
Declare the following variables in the class:
PRivateudtCoatAsCoatType
This defined private variable is used to save the Coat type attribute that will be added. Note that the udtCoat variable is neither a string nor an integer, but our own defined enumeration type CoatType.
When the class CDog is opened, select the "AddProcedure" command in the "Tools" menu and the corresponding dialog box will pop up;
Type Coat in the Name edit box;
Check the "Property" option button and click [OK].
The system automatically generates the following code framework:
But this is not the framework we need. In the code, the "Variant" variable type can receive and process any type of data. In the CDog class we defined, the last attribute is Age, which can only accept integers. But now the attribute needs to be able to receive the data type in the CoatType list, so the following modifications need to be made:
Change all "Variant" in the generated code to "CoatType";
Then, add some code that actually handles the properties.
In the property's Get procedure, add the following code:
In the property's Let procedure, add the following code:
Switch to Form1;
Change the code of the Command button to:
Now start typing: MyDog.Coat=
The magic happens, when you hit the "=" key, a list of possible options appears, from which we can choose one.
Complete the typing of the code: MyDog.Coat=ShortCrewCut
Next, we will get the value of the Coat property. If we were now simply displaying property values in a message dialog box, we would simply return the value of the selection. For example, if ShortCrewCut is selected, its property must return 2. If you don’t believe it, you can give it a try!
But here is another method, which is to use the If-Then statement to determine Coat:
Add the following code after the existing code for the Command button:
The code here simply determines the Coat property value and displays the corresponding message dialog box. Of course, the "SelectCase" statement can also be used here.
Finally, we add one last statement to free the computer's memory:
Add the following code after the existing code for the Command button:
Press F5 to run the program and click the Command button to test it out.
What was the result? ->