The COM Course - Part 1, Page 5
Sometimes you have certain properties in a class that could be dealt with better.
Say you have four fixed types of customer Enterprise, Medium, Small and New. Or three different types of searching methods in your Search class FloppyDisks, HardDisks and Network. Wouldn't it be nice to pick one of these options from a list, rather than setting your property to some obscure number or string of text?
You can with crazy lil' things called enumerations.
Let's add a little code to our class:
- Insert the following into your CDog class:
Public Enum CoatType BigAndShaggy = 1 ShortCrewCut = 2 PoodleStyleAfro = 3 Unknown = 4End Enum
This 'enum' is an enumeration. In other words, it's a list of possible options. The numbers next to these possible options don't have to be there they just say that BigAndShaggy is represented by the number one; ShortCrewCut equals number two, etc.
Top Tip: The numbers next to an enumeration are useful if you want to put information into a database. As 'BigAndShaggy' really translates into the number 1, you could insert it direct into a database number field. That means you have the power of using string options in code yet can still maintain a great database design.
So, we've created a list of Dog coat types. Now let's add another property to our class to use these types:
- Declare the following variable in your class:
Private udtCoat As CoatType
This is the private variable that will hold the Coat information on behalf of our soon-to-be-added property. Notice how the variable is declared not as a string or integer but as the CoatType enumeration we created, our own 'user-defined data type'.
- With your CDog class open, click 'Tools', 'Add Procedure'
I'm now going to show you a great way of creating your property procedures superfast.
- In the 'Name' box, type Coat
- Select the 'Property' option button, then click OK
The following skeletal code should be automatically generated for you:
Public Property Get Coat() As VariantEnd PropertyPublic Property Let Coat(ByVal vNewValue As Variant)End Property
Great! But this isn't exactly what we need. Right now, this code accepts and passes back 'variants', which can be just about anything. If you remember, our last property Age accepted integers. This one wants to accept anything in the CoatType list.
- Change everything that says 'Variant' in the generated code to 'CoatType'
Now let's add a little code to actually handle the property:
- In the Property Get procedure, add the following code:
Coat = udtCoat
- In the Property Let procedure, add the following code:
udtCoat = vNewValue
- Flick back to Form1
- Change the code behind your Command Button to:
Dim MyDog As CDogSet MyDog = New CDogMyDog.Name = "Billy"
- Now start to type in: MyDog.Coat =
Do you see what happens? As you press the 'equals' key, a list of possible options appears. You can select any of these. Except the Afro one. Billy certainly doesn't have Afro hair. Nor Big and Shaggy come to think of it. In fact, just select the short crew cut option.
- Finish typing the code: MyDog.Coat = ShortCrewCut
Next up, let's retrieve the value of the Coat property. Now, if we simply display the value in a message box, we'll only get back the number of the option we selected. In other words, if you selected ShortCrewCut, the property will return a 2. Try it!
But there's another way of doing it by comparing the Coat with an If-Then:
- Append the following code to the existing behind your Command Button. Make sure you type this out yourself to see the full effect:
If MyDog.Coat = BigAndShaggy ThenMsgBox "You have a big, bouncy, bushy pup!"ElseIf MyDog.Coat = PoodleStyleAfro ThenMsgBox "Your pooch is pretty, petit and pooch-like!"ElseIf MyDog.Coat = ShortCrewCut ThenMsgBox "Your dog is full of oomph, oomph and more oomph!"ElseIf MyDog.Coat = Unknown ThenMsgBox "I have no idea about your dog. I don't think " & _"you do either!"End If
Here, our code simply evaluates what the Coat property is equal to. It then displays a relevant message box.
Top Tip: You could also do a 'Select Case' here but I'm trying to keep the code simple. Oh yeah, and I'm an idle sloth.
And finally, to be all nice and tidy let's add that one final line of code to free up computer memory:
- Append the following code to the existing behind your Command Button:
Set MyDog = Nothing
- Test your application by pressing F5, then hitting your button
See what happens? That's enumerations for ya.
Page 5 of 8