fcp
asked on
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
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
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
"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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
>>the other one is late binding
to be called "late binding", the object must be declared as OBJECT!
to be called "late binding", the object must be declared as OBJECT!
ASKER
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.
Thanks to all, mates.
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."
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."
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.
ANd the performance hit is the big problem, here, not the memory usage.
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