October 23, 2016
Hot Topics:

Beginning Objects in VB - Part 2

  • November 19, 2002
  • By Sam Huggill
  • Send Email »
  • More Articles »

In this day and age, you need to know how to create an object through code. It's rather like declaring a variable, in fact it is in a sense, but instead of declaring it as a data type (e.g. String), you declare it as an object (in our case, Class1).

You can declare an object using either the Dim, Private or Public keyword. As with other variables, the Private and Public keywords can only be used in the General Declarations procedure.

You will probably know that declaring something as Public allows anyone to access the variable, and declaring it as Private only lets the current module of code access the variable.

Anyway, stop blabbing on!

The sort of variable declaration that you're used to is something like:

Private m_strName As String

Well, here is a simple object variable declaration:

Private m_clsClass1 As Class1

Easy. But if you remember from the previous part, we used a different piece of code to actually create the code:

Set m_clsProject1 = New Class1

Help!!!! What on earth does this do??

Well, don't worry, I'm here to hold your hand all the way (ok, so your mouse will have to hold your hand in my absence)

What that line of code does is create a new 'instance' of the object Class1. It means that you can now do some stuff with the object, whereas before if you tried doing anything you would have had the following error message:

Object or With Variable Not Set

Don't you just love VB error messages?

(Me: Well, no actually)
(You: Weren't you being sarcastic?)
(Me: Good point. DOH!)

Anyway, getting back to the point, you need to actually create a New instance of the object to be able to use it. But if you thought you just had to do it that way every time, then you were wrong....

VB allows you to use the New keyword when you declare an object variable:

Private m_clsClass1 As New Class1

When your program starts, the object is automatically created so you can use its methods etc immediately. This may sound really brill, but let me point out a few probs with it.

If your object fires up a connection a database and gets some records when you start, or if your object simply contains a large amount of code, then you may be causing the users machine to run slower then needed. If you don't make a call on the object for a few minutes, then you are wasting memory by having the object in memory.

So, what can you do? Well, simply avoid using the New keyword when declaring the object. Every time you want to use it, create a new instance of it and then destroy it (Destroy it? You mean bash my computer over the head?)

No, not quite. When dealing with objects (as we'll see later on) you use the Set keyword to perform some operations. When we want to destroy an object, we set it to nothing, quite literally:

Set m_clsClass1 = Nothing

This way, we only have it loaded in memory when we need to use it. But, if I take the example used earlier of the database, it may be better to keep the object 'alive' as to speak for longer because invoking it several times may be the slower option. It's really a case of working out which is going to be more efficient.

If you have a lot of users using a database object for example, then you may want to open and close connections for each user. This is called object 'pooling' where you pool all your resources together and dish them out when requested. Microsoft makes this easier by giving you Microsoft Transaction Server, but I won't go into the complicated ins and outs of that beast now.

So, as you can see object dudes, it's down to you to work out when you need to new object and when you can destroy it.

Next we move onto registering your object...

Page 2 of 4

Comment and Contribute


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



Enterprise Development Update

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

Sitemap | Contact Us

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