ActiveX Control Tutorial - Part 2, Page 4
You've played around with properties before. Every time you say...
Text1.Text = "Hello World!"
... you're setting a property. And every time you say...
... you're reading a property.
Because you're now a supercool ActiveX programmer, you're going to have to provide all this functionality to your end user too.
Now creating properties can get a little bit difficult so let me try to explain. When you set a property in code, it runs one bit of code. When you retrieve a property in code, it runs another piece of code.
So even though you may be setting and retrieving the same property, behind the scenes two different pieces of code actually fire up. And as you're now working behind the scenes, you need to know about them.
- Open the code window
- Click Tools, Add Procedure
- In the Name box, type 'AcceptType'
- Click the Property option button
- Hit OK
You should notice the following added to your code window:
Public Property Get AcceptType() As VariantEnd PropertyPublic Property Let AcceptType(ByVal vNewValue As Variant)End Property
Here we have a let and a get. The let piece of code runs when a user tries to set the value of our AcceptType property. And the get piece runs when someone tries to retrieve the value of AcceptType.
Apart from that rather important difference, this property lark isn't that dissimilar from regular subs and functions. You can imagine that the let piece of code is a sub, that accepts the new value of AcceptType in the passed vNewValue. And the get piece of code works just like a function to send information back to the user, you simply set AcceptType to a specific value.
[Reader: Yeah, in the same way the King of Egypt followed Moses...]
Let's take a peek at an example property let and get pair...
Dim strUserName as StringPublic Property Get UserName() As VariantUserName = strUserNameEnd PropertyPublic Property Let UserName(ByVal NewUserName As Variant)strUserName = NewUserNameEnd Property
Here, we have a local variable called strUserName.
When the property is set...
MyControl.UserName = "John"
... the let procedure runs and sets the value of strUserName.
When the property is retrieved...
... the get procedure runs and returns the value of strUserName.
That's exactly how most property procedures work. But we're using enumerations to make life easier for our end user... yet alas, slightly more difficult for ourselves.
- Change the skeletal property procedures Visual Basic created for us to something like this:
Public Property Get AcceptType() As CharacterTypeEnd PropertyPublic Property Let AcceptType(ByVal CharType As CharacterType)End Property
The only important thing we've done here is to change 'Variant' to 'CharacterType'. If the user passes a variant, they could be sending anything our way. This way we're saying you will pass us one of the CharacterType options and nothing else. Haha, the power!! Muhahaha!
Top Tip: If you wanted the user to just supply a True or False value, you could always use 'As Boolean' in place of 'As CharacterType'. Or perhaps 'As Integer' or 'As String'.
Now let's add actual code to bring the property procedure to life:
- In the General Declarations section, declare the following:
Dim m_CharType as CharacterType
- Add a little more code to our AcceptType property procedures, changing them to:
Public Property Get AcceptType() As CharacterType AcceptType = m_CharTypeEnd Property
Public Property Let AcceptType(ByVal CharType As CharacterType)m_CharType = CharTypeEnd Property
Think about it for a short while. Do you understand what's happening here? The m_CharType variable stores the actual value.
Top Tip: You're probably wondering why we're bothering to code all these Property Get/Let statements when it seems to do just the same job as a public variable. The difference here is that you have the power to add code behind your Get/Let statements, whereas with a public variable you have no control whatsoever. In other words, you could have a property that accepts some usernames, but if they choose 'Admin', you could tell them to go swizzle. Erm, programmatically speaking, of course.
- Now add the ConvertCase property to your project:
Dim m_CaseType As IntegerPublic Property Get ConvertCase() As CaseType ConvertCase = m_CaseTypeEnd PropertyPublic Property Let ConvertCase(ByVal CsType As CaseType) m_CaseType = CsTypeEnd Property
Are you getting acquainted with properties yet? Hah, you see they're not that frightening in fact I was more petrified the last time I watched that epic of epic's, Hoger and Rammerstein's the Sound of Music.
[Ed: So were we it was your 538th visit!]