Solved

How to create object variables in VB6

Posted on 2004-08-24
3
9,701 Views
Last Modified: 2008-01-09
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
Comment
Question by:rbali2004
[X]
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
3 Comments
 

Expert Comment

by:manisnkr
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.

0
 
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

Regards,

Idle_Mind
0
 
LVL 3

Accepted Solution

by:
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …
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…

728 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