Trivial (for u) q. about classes
-
Hi, I know, I have a very stupid question, but please accept, that I've just created my first class in my life. And the question is, Must I set some of the variables declared in class to nothing or do something to free the memory in the end? (If so, please specify how). And also, when i write Dim myFirstClass as New FirstClass in the general declarations in page code, must I set this variable to nothing? Thanks
-
Hi, I know, I have a very stupid question, but please accept, that I've just created my first class in my life. And the question is, Must I set some of the variables declared in class to nothing or do something to free the memory in the end? (If so, please specify how). And also, when i write Dim myFirstClass as New FirstClass in the general declarations in page code, must I set this variable to nothing? Thanks
Anonymous wrote: Must I set some of the variables declared in class to nothing or do something to free the memory in the end? No, you don't have to. The Garbage Collector will remove the objects when there are no references to them. Some classes have a
Dispose()
method and when you are finished with them, you should callDispose()
to clean up. This is because these classes contain references to native objects in the operating system that cannot be garbage collected. Of course, if you forget then it is normally no big deal, the finalizer will call dispose for you - however it does mean that the memory or resources will not be freed until the garbage collector gets to it. This could cause problems for things that are scarce resources. Anonymous wrote: And also, when i writeDim myFirstClass as New FirstClass
in the general declarations in page code, must I set this variable to nothing? I'm not a VB.NET developer, but if I understand it correctly, the line of code you give will create a reference for an object and assign a new instance of the object to the reference. If you then set Nothing to the reference immediately after then you are wasting the processor's time by instantiating an object then immediately throwing it away. Does this help?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
-
Anonymous wrote: Must I set some of the variables declared in class to nothing or do something to free the memory in the end? No, you don't have to. The Garbage Collector will remove the objects when there are no references to them. Some classes have a
Dispose()
method and when you are finished with them, you should callDispose()
to clean up. This is because these classes contain references to native objects in the operating system that cannot be garbage collected. Of course, if you forget then it is normally no big deal, the finalizer will call dispose for you - however it does mean that the memory or resources will not be freed until the garbage collector gets to it. This could cause problems for things that are scarce resources. Anonymous wrote: And also, when i writeDim myFirstClass as New FirstClass
in the general declarations in page code, must I set this variable to nothing? I'm not a VB.NET developer, but if I understand it correctly, the line of code you give will create a reference for an object and assign a new instance of the object to the reference. If you then set Nothing to the reference immediately after then you are wasting the processor's time by instantiating an object then immediately throwing it away. Does this help?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
Yes, you made it more clear for me, thanks.