February 27, 2021
Hot Topics:

Beginning Objects in VB - Part 1

  • By Sam Huggill
  • Send Email »
  • More Articles »

So far we have discussed objects as controls in VB (the Textbox and RichTextBox - you should be remembering these by now!)

In this example, we're going to make a different kind of object. When you make a control, it has a user interface (that means you can visually see something on the screen, such as the textbox itself). Another kind of object is one that doesn't have a user interface, and we call this a DLL (of Dynamic Link Library if you want to impress your friends).

To make one of these, click File, New Project and select ActiveX DLL:

This makes a new project with one empty class module in it. On the properties pane, make sure that the Instancing property is set to 5 - MultiUse. This allows us to compile the DLL and expose the class module to other programs (such as the object browser (This is starting to get complicated! (Espically with all these brackets :)))

We only want this example to be simple, so why don't we base it around the cheesy Hello World project we talked about earlier on.

Lets add a new method called SayHello that pops up a message box saying Hello World! on it.

But before you rush off to start coding. let me show you a useful tool that VB has. It's called the VB Class Builder. To open it, click Project Add Class module and select VB Class Builder:

You'll probably get a message box telling you that the project contains a class module not generated by the Class Builder, but just ignore it and press ok.

We want to add a new method, so click on Class1 and select File, New, Method. Enter a name as SayHello and click OK. You should now see the item SayHello in the right hand pane (contact me if you don't, or simply scream :)

As that's all we want, click File, Exit and Yes to update the project.

As you will see, we didn't achieve much there! Two lines of procedure code - not much for all that faffying around. But as we continue through this series, you will get a handle on the uses of the Class Builder.

We only want a message box to show up, nothing too serious. Add this line of code:

Msgbox "Hello World!"

The procedure should look like this:

Public Sub SayHello()
    MsgBox "Hello World!"
End Sub

Time to build! Save the project, and a click File, Make Project1.DLL.

There you go! You've just complied your first object! (OK, so maybe this isn't your first time, but who cares?)

Now all we need to do is test it. Lets add another project - File, Add Projcet, Standard EXE. (Uhh? How do we load up the object - it doesn't appear under the components dialog box)

Good question (I dunno who asked it, but it was good :) Because a DLL is not a control, we need to make a reference to it (because we are not loading any kind of user interface)

So, click File, References and scroll down and select Project1:

(If it's not there, click on the browse button and locate the Project1.DLL)

Click ok and your object is now loaded. Just to make sure, press F2 to load up the object browser and select Projec1 from the combo box. See the SayHello method there? Good.

Now lets do some cool coding and call the SayHello method:

- Add a declaration of the object:

Dim m_clsProject1 As Class1

- Now, under the Form_Load event, create the object:

Set m_clsProject1 = New Class1

And call the SayHello method:


Run the project and see the message box appear! (If you get an error let me know and I will sort it out :)

Wow! Is that the time? I better be on my way for now, but next week I'll come back to looking at what we have just done (creating objects and all that stuff) but for now, download the sample files:

Download the files!

And let me know what you thought of this article! (Remeber, click Post Feedback Now just below this text)

Have a nice week! (or was it Have a nice day? I'm losing my memory at the moment - more sleep required!)

C ya next week!

Page 5 of 5

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