Click here to Skip to main content
15,888,006 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Ashfield25-Jun-08 21:29
Ashfield25-Jun-08 21:29 
AnswerRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Christian Graus25-Jun-08 22:15
protectorChristian Graus25-Jun-08 22:15 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Sakthivel P25-Jun-08 22:40
Sakthivel P25-Jun-08 22:40 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Christian Graus25-Jun-08 22:47
protectorChristian Graus25-Jun-08 22:47 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Sakthivel P25-Jun-08 23:08
Sakthivel P25-Jun-08 23:08 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Christian Graus25-Jun-08 23:35
protectorChristian Graus25-Jun-08 23:35 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Sakthivel P26-Jun-08 0:23
Sakthivel P26-Jun-08 0:23 
AnswerRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Guffa26-Jun-08 1:07
Guffa26-Jun-08 1:07 
Let's see exactly what happens with your code...

Dim MyCollect as Collection
Set MyCollect = new Collection
Dim Data as Integer


So far nothing surprising. A reference variable is declared, an object is created and it's reference is stored in the variable, and an integer variable is declared.

Data = 1
MyCollect.Add Data


Here it starts to get interresting. You can only store reference types (i.e. objects) in a collection. As an integer is a value type, it can not be stored in the collection, so what happens is that a new object is created automatically, and the integer value is copied to that object. This is called boxing.

Data = 2
MyCollect.Add Data
Data = 3
MyCollect.Add Data


Here you are saved by the boxing. If Data would have been a reference type, the collection would contain a reference and not a separate object, so you would change the contents of the only one object, and you would have ended up with a collection that contained three references to the same object. The boxing creates separate objects for each value, so you get three separate objects in the collection.

What happens after this statement is where it gets really interresting. As the collection isn't used any more in the code, it's now eligible for garbage collection. The garbage collector can determine that the reference to the collection is never read after this, so the reference is deemed inactive from here on. Eventhough the variable contains a reference to the collection, it doesn't count as the reference is inactive, so there are no longer any active references to the collection.

If a garbage collection would happen at this point in the code, the collection and all the objects that it contains could (and most likely would) be garbage collected.

Set MyCollect = Nothing


This statement actually serves no purpose what so ever in your code. As the garbage collector knows that the reference is alredy inactive at this point, you can put whatever you like in the reference, and it will not change how any objects are garbage collected in any way.

Despite everything, the person most likely to be fooling you next is yourself.

modified on Thursday, June 26, 2008 7:23 AM

GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Sakthivel P26-Jun-08 5:36
Sakthivel P26-Jun-08 5:36 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Guffa26-Jun-08 16:09
Guffa26-Jun-08 16:09 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
supercat927-Jun-08 7:07
supercat927-Jun-08 7:07 
GeneralRe: Set nothing to VB Collection class object, is it really removes items internally. Pin
Guffa27-Jun-08 13:01
Guffa27-Jun-08 13:01 
QuestionDowntime Management - Client-Server Environment Pin
Anoop Brijmohun25-Jun-08 20:25
Anoop Brijmohun25-Jun-08 20:25 
AnswerRe: Downtime Management - Client-Server Environment Pin
Mycroft Holmes26-Jun-08 16:56
professionalMycroft Holmes26-Jun-08 16:56 
GeneralRe: Downtime Management - Client-Server Environment Pin
Anoop Brijmohun26-Jun-08 20:31
Anoop Brijmohun26-Jun-08 20:31 
GeneralRe: Downtime Management - Client-Server Environment Pin
Mycroft Holmes27-Jun-08 0:53
professionalMycroft Holmes27-Jun-08 0:53 
QuestionVB6 rs.close error Pin
Amit Battan Ror25-Jun-08 19:38
Amit Battan Ror25-Jun-08 19:38 
AnswerRe: VB6 rs.close error Pin
Amit Battan Ror25-Jun-08 19:42
Amit Battan Ror25-Jun-08 19:42 
AnswerRe: VB6 rs.close error Pin
~Khatri Mitesh~25-Jun-08 20:39
~Khatri Mitesh~25-Jun-08 20:39 
GeneralRe: VB6 rs.close error Pin
Guffa26-Jun-08 1:11
Guffa26-Jun-08 1:11 
AnswerRe: VB6 rs.close error Pin
Guffa26-Jun-08 1:17
Guffa26-Jun-08 1:17 
AnswerRe: VB6 rs.close error Pin
supercat927-Jun-08 7:26
supercat927-Jun-08 7:26 
GeneralRe: VB6 rs.close error Pin
Amit Battan Ror29-Jun-08 18:10
Amit Battan Ror29-Jun-08 18:10 
QuestionCouldn't resolve default property of object Me. Pin
AR Reddy25-Jun-08 19:33
AR Reddy25-Jun-08 19:33 
AnswerRe: Couldn't resolve default property of object Me. Pin
nishkarsh_k26-Jun-08 0:29
nishkarsh_k26-Jun-08 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.