How to create object variables in VB6

Posted on 2004-08-24
Medium Priority
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

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 160 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline

589 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