February 27, 2021
Hot Topics:

The COM Course - Part 1

  • By Karl Moore
  • Send Email »
  • More Articles »

In this section, we'll dive straight into the practical. We'll be creating our first COM object, using it, then considering how we can improve it.

So let's get started:

  • Launch Visual Basic
  • Create a new 'Standard EXE'

Now COM objects are always based on classes. And classes are just chunks of code, a lot like the code in a module, except it's used in a different way.

  • Click 'Project', 'Add Class Module'
  • In the prompt that appears, select 'Class Module' and click 'Open'

The Project Explorer should now show that in addition to Form1, your Project1 also contains Class1. Whoah, too many 1s, dude.

Next, let's be all neat and tidy and change the name of our class:

  • In the Properties window, change the Name property of Class1 to: CDog

Top Tip: Just as you use certain prefixes for certain objects ie, 'txt' for a Text Box and 'frm' for a Form classes are usually prefixed with a capital 'C' or lower case 'cls'. I prefer the former.

Now that big blank bit of white in front of you is going to be your home for the next few minutes. This is where you write your class code, the stuff that COM objects are driven by.

Let's test it out:

  • Declare the following variable in your CDog class:
Public Name As String

Erm, well maybe that didn't take a few minutes but you've done all necessary to take your first step onto becoming a real COM programmer.

Vot haff ve crrreated? Good question. Let's find out:

  • Open Form1
  • Add a Command Button to your Form
  • Insert the following code behind your button:
Dim MyDog As CDogSet MyDog = New CDogMyDog.Name = "Billy Moore"MsgBox MyDog.NameSet MyDog = Nothing

Let me explain what's happening here.

Dim MyDog As CDog

This line of code is telling Visual Basic to set aside a bit of space for the grand entry of CDog. We can't use this object yet though - that comes with the next line of code.

Set MyDog = New CDog

This creates a new instance of CDog. This means the previously 'empty' MyDog template is now a CDog object we can work from. Think of it as creating a new cookie based on the CDog cookie cutter.

MyDog.Name = "Billy Moore"MsgBox MyDog.Name

The first line here sets the Name variable of MyDog, whilst the second displays it in a message box. And finally:

Set MyDog = Nothing

This code simply sets MyDog equal to Nothing. That means the cookie you originally created is eat up by some greedy overweight teenager and completely disappears from your computer. It's called being neat.

* Press F5 to run and test your application

Did it work? Jolly good fun. But at the moment, it's probably a little difficult to tell the difference between, say, a standard module and a class module. Well, the next sample will show us:

  • Change the code behind your Command Button to:
Dim MyDog As CDogSet MyDog = New CDogDim MyDog2 As CDogSet MyDog2 = New CDogMyDog.Name = "Billy Moore"MsgBox MyDog.NameMyDog2.Name = "Sadie Moore"MsgBox MyDog2.NameSet MyDog = NothingSet MyDog2 = Nothing

Here, we see exactly the same as our last chunk of code except we're using two different things, MyDog and MyDog2. Both of these objects are based on our CDog class yet are completely independent of one another.

That goes hand in hand with our cookie cutter analogy. Our code is the cookie cutter and you can make as many independent cookies as you want from it, all based on that one cutter.

Let's test our application:

  • Press F5 and test your application

See what happens? This time, you get two message boxes appearing one saying Billy Moore, the other Sadie Moore both exceptionally cute Boxer dogs, I might add.

Now it's worth noting that most classes are built around real life objects. So, each dog already has a Name. What other properties could we add?

  • Open up Class1
  • Declare the following public variable:
Public Age As Integer
  • Open up Form1
  • Change the code behind your Command Button to:
Dim MyDog As CDog

Set MyDog = New CDog Dim MyDog2 As CDogSet MyDog2 = New CDog MyDog.Name = "Billy Moore"MyDog.Age = 4MsgBox MyDog.Name & " is " & MyDog.Age & " years old"MyDog2.Name = "Sadie Moore"MyDog2.Age = 7MsgBox MyDog2.Name & " is " & MyDog2.Age & " years old"Set MyDog = NothingSet MyDog2 = Nothing

This code is exceptionally similar to the last lot, except here we're using the new Age variable.

  • Press F5 and test your application

You should receive a message box displaying both the name and age of each lovable pooch.

Now try to set the age of one of the dogs to 1,000. Or perhaps 30,000.

See what happens? Bugger all. That's because an integer variable can store any value up to 32,767. But it doesn't make sense for a dog to be 30,000 years old (unless it's an exceptionally well-kept one).

So how do you handle situations like this?

Page 3 of 8

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date