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

A really stupid question

Ok, i've fully understand a little thing:
what's the difference between
"Dim cA as new ClassA"
and
"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...

Thanks
0
fcp
Asked:
fcp
1 Solution
 
JohnMcCannCommented:
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
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
0
 
Arthur_WoodCommented:
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.

AW

2)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jayeshshahCommented:
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.

K'Regards

Jayesh
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>the other one is late binding

to be called "late binding", the object must be declared as OBJECT!
0
 
fcpAuthor Commented:
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.
0
 
DiveblueCommented:
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."
0
 
Arthur_WoodCommented:
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.
0

Featured Post

Technology Partners: 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!

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