A really stupid question

Posted on 2003-03-13
Medium Priority
Last Modified: 2010-08-05
Ok, i've fully understand a little thing:
what's the difference between
"Dim cA as new ClassA"
"Dim cA as ClassA"
"Set cA = new ClassA"
I always us the second way, with a "Set cA=nothing" when i need no more the class, but i'm curious about the differences...

Question by:fcp
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: 8130135
Dim cA as new ClassA

If this is a module/class level variable it is slower during initalization.

Where as

Dim cA as ClassA

and this called somewhere else

Set cA = new ClassA

Would be faster to initalizze
LVL 70

Expert Comment

by:Éric Moreau
ID: 8130225
When you use:
"Dim cA as new ClassA"

The instance is not created until you first use the object variable and the compiler add this line to your code everytime your object variable is used (this is why this method is slower):

if cA is nothing then set ca = New ClassA
LVL 44

Accepted Solution

Arthur_Wood earned 200 total points
ID: 8130270
Dim cA as New ClassA

will cause VB 6 to CREATE the instance the very first time the variable cA is used, but it also requires VB to check to see if there is ALREADY an existing instance, EVERY TIME cA is used, and that can create several problems:

1) it can lead to a SIGNIFICANT perdormance hit, and
2) if an instance of cA DOES not exist, it will be automatically recreated, so that code like:

set cA = Nothing

accomplishes NOTHING (pardon the pun), since the next time cA is encountered, it will be re-created, as if if already existed.

My advice is to continue, as you say you already do, using the form:

Dim cA as ClassA

.  set cA = New ClassA
set cA = Nothing

that way you
1) control EXACTLY when cA is created and destroyed
2) save the unn=ecessary performance hit.


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!


Expert Comment

ID: 8130281
by using the first one u r implementing something called as early binding and the other one is late binding.

In short the first one should not be used especially for the module/form level variables since then these would always be loaded in the memory till the form is visible or is loaded in the memory.


LVL 70

Expert Comment

by:Éric Moreau
ID: 8130291
>>the other one is late binding

to be called "late binding", the object must be declared as OBJECT!

Author Comment

ID: 8131792
Well, even if all answers are right, i judge this one the best, for completition and simplicity (english is not my language, and this answer is the one i better understand).

Thanks to all, mates.

Expert Comment

ID: 8131829
Arthur has the most correct answer so far, with the exception that his statement (below) is misleading.
set cA = Nothing does accomplish somthing. it removes
cA from memory (assuming there are no other references to the object). Therefore, if cA is re-created later, its internal data is reinitialized, and the Class_Initialize event is called. (not nearly the same as accomplishing nothing).

"set cA = Nothing accomplishes NOTHING (pardon the pun), since the next time cA is encountered, it will be re-created, as if if already existed."
LVL 44

Expert Comment

ID: 8133006
howeve, that statement was made in the contest of dim cA as New ClassA.  Yes, it removes cA from memory briefly, but the very next reference to cA recreates it IMMEDIATELY.

ANd the performance hit is the big problem, here, not the memory usage.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

770 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