September 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The COM Course - Part 1

  • November 19, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

Properties work in much the same way as public variables, except they allow you a lot more control.

Regular properties consist of two property 'routines', a Get and a Let. Both of these work together to provide one main property. Let's look at a chunk of sample code:

Private intAge As IntegerPublic Property Get Age() As Integer    Age = intAgeEnd PropertyPublic Property Let Age(ByVal vNewValue As Integer)    intAge = vNewValueEnd Property

The above chunk of code works much like the Age variable. When someone does something like:

MyDog.Age = 4

... the Let property runs and the number four is passed as vNewValue. This is then stored in the private intAge variable. When someone does something like:

MsgBox MyDog.Age

... to retrieve the property, the Get property is run and the value of intAge is returned. So let's think about that:

  • Get Property Runs when someone wants to "get" the value
  • Let Property Runs when someone wants to "let" your property equal a value

So far, I've only demonstrated these properties working exactly like standard variables (except with a lot more code). However they also allow you to have more control over what goes into your property and what does not.

Let's edit our class:

  • Remove the Age variable from your CDog class
  • Insert the following code:
FACE="Courier" SIZE=2>

Private intAge As Integer

Public Property Get Age() As Integer    Age = intAgeEnd PropertyPublic Property Let Age(ByVal vNewValue As Integer)    If vNewValue <= 50 Then        intAge = vNewValue    End IfEnd Property

The only real change here to our original chunk of example code is the Property Let routine. Here, we've done a little checking. So imagine our user attempts to do a:

MyDog.Age=30

... our Property Let routine runs, passing thirty as vNewValue. In code, we than evaluate vNewValue to check whether it is less than or equal to fifty. Our thirty figure is, so in this instance intAge would be set to thirty. And that's that.

If it was over fifty, nothing would happen. The property would just exit and no values would be set. Naturally, you could decide to raise an error or display an exclamatory message box, but I'm feeling all kind today.

  • Switch back to the code window behind Form1
  • Click onto the first line of code where we set the Age property, and press F9:
MyDog.Age = 4
  • Click onto the first line of code where we get the Age property, and press F9:
MsgBox MyDog.Name & " is " & MyDog.Age & " years old"

Now let's test our application:

  • Press F5 to run your program
  • Hit your Command Button

The code should pause on the lines of code on which you pressed F9 and added a 'break point'.

  • When the code pauses, slowly observe and step through each line by pressing F8

Do you see how it all works? Notice how the Age property goes through the 'get' and 'let' routines? That's properties in a nutshell for ya.

And in the next section, we're going to learn not only a way of making properties more user friendly, but also how you can create them in no time!





Page 4 of 8



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel