How to create object variables in VB6

Posted on 2004-08-24
Last Modified: 2008-01-09
I have a question. What is the difference b/w the folliwng two syntaxes. I have a dll project named sortClass that contains a class called sortFuncs. When i add a reference to the class in vb6, i can use the following 2 syntaxes to create an object of that class. However, what is the difference between the two?

dim myobj1 as new sortfuncs
dim myobj1 as new sortClass.sortFuncs

MY second question is that while reading a vb .NET book I noticed that there were certain variables that were declared without the NEW keyword and certain had the new keyword. For example,
Dim myarraylist as arraylist

While in another example, an arraylist was declared as
Dim myarraylist as new arraylist

Isthere any difference?

Question by:rbali2004
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 11883199
dim myobj1 as new sortfuncs
dim myobj1 as new sortClass.sortFuncs

The class functions can be accessed only thorugh the respective class,if the class is not available in the same project. If the class is available in the same project then it acts like a global function.

Object variables can be used only if it is referenced with a New keyword.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11883436
>> Dim myArrayList As ArrayList

myArrayList has been declared to be of type ArrayList, but has not yet been instantiated.  Therefore, you cannot perform any actions on myArrayList yet.  It is just a placeholder that can (but does not yet) point to an ArrayList in memory.

>> Dim myArrayList As New ArrayList

myArrayList has been declared to be of type ArrayList and now points to an instance of ArrayList in memory.  You may immediately perform actions on myArrayList.  A more formal declaration would look like this:

    Dim myArrayList As ArrayList = New ArrayList

Some prefer to see it on two lines like this:

    Dim myArrayList As ArrayList
    myArrayList = New ArrayList



Accepted Solution

travisjhall earned 40 total points
ID: 11888815
First question...

dim myobj1 as new sortfuncs
dim myobj1 as new sortClass.sortFuncs

The second form specifies the module which gives the sortFuncs object type. The first leaves this unspecified. Much of the time, there is no real difference between the two. However, the second form becomes important when two different modules each implement a sortFuncs object type. If you use the first form, the compiler can't tell which one you want to use. It will probably give a compile error, and if it doesn't I wouldn't want to be trying to guess which object type it uses.

Second question....

If you use the New keyword when you define (Dim) an object variable, your program will automatically instantiate an object of the appropriate type and set the variable to that object the first time the variable is used. If the variable is never used, the object never gets instantiated.

This can be very convenient, because you know that the object will be ready whenever you happen to use it. However, there's a bit of a problem with doing it that way. Because program flow can be complex, you can't tell at compile time when the first use of that object variable will be. That means that the compiler has to safeguard every reference to that variable. Essentially, every use of the variable in your code will have a check to see whether the object has been instantiated yet inserted immediately before it. So you go from:

Dim myarraylist As New arraylist


Dim myarray As New arraylist
If IsEmpty(arraylist) Then Set arraylist = New arraylist
If IsEmpty(arraylist) Then Set arraylist = New arraylist
If IsEmpty(arraylist) Then Set arraylist = New arraylist

and then that is what gets compiled into your executable (or DLL, or whatever). Doesn't look terribly efficient, does it? And it's not. So it can be good for small program using an object variable only once or twice, but not so great when your variable is referenced a thousand times. Thus, I generally avoid As New. I'd usually rather set the variable myself, know exactly when the object is instantiated, and avoid the unnecessary checks. Thus:

Dim myarray As arraylist
Set myarray = New arraylist

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question