Solved

How to create object variables in VB6

Posted on 2004-08-24
3
9,691 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
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 85

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now