• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9705
  • Last Modified:

How to create object variables in VB6

Hi,
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?

Thanks
Rafay
0
rbali2004
Asked:
rbali2004
1 Solution
 
manisnkrCommented:
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.

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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

Regards,

Idle_Mind
0
 
travisjhallCommented:
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
arraylist.somemethod
arraylist.someothermethod
arraylist.yetanothermethod

to:

Dim myarray As New arraylist
If IsEmpty(arraylist) Then Set arraylist = New arraylist
arraylist.somemethod
If IsEmpty(arraylist) Then Set arraylist = New arraylist
arraylist.someothermethod
If IsEmpty(arraylist) Then Set arraylist = New arraylist
arraylist.yetanothermethod

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
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now