September 19, 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 »

So, we've already added variables, properties and enumerations to our classes. What else can you do with 'em?

Well, for a start, you can add Subs. That's right, bog standard ol' Subs, as regularly used in most Visual Basic programs. Let's add one now to demonstrate their power:

  • Insert the following into your CDog class:
Public Sub Bark()    MsgBox "Woof! Woof!"End Sub

And that's it! Here we have a regular sub inside our class that you can run quite simply with a command like: MyDog.Bark.

Let's try it out:

  • Replace the code behind your Form1 Command Button with:
Dim MyDog As CDogSet MyDog = New CDogMyDog.Name = "Billy"MyDog.BarkSet MyDog = Nothing

Notice how after you press the period following MyDog, the Bark Sub is yellow, whilst the properties are blue and white? That's to help you distinguish Bark as a method of your MyDog object.

  • Press F5 and test your code

In addition to subs, you can also add functions to your code. I'm not going to demonstrate it here, but why not have a go yourself? They work just the same as regular functions, except they're inside a class. Don't forget that all Subs and Functions can also have their own parameters.

Now let's imagine I wanted my class to have a Sleep method. I'd call this Sub each time I wanted my pup to take a cat- or rather, a dog-nap.

But how would I know when my little bundle of joy has woken up? Well, that's a good question. And the answer is events.

How do you know when someone types into your Text box? There's the Change event. How do you know when someone clicks on your Command Button? There's the Click event. How do you know when your pooch wakes up? Enter centre stage: the Awake event.





Page 6 of 8



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel