form variables and scope

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

if I have

sub button_onclick

dim f as new form2
f.show

end sub


then I thought that at the 'end sub' the variable (or object reference) 'f'
fell
out of scope and so the form should be garbage collected

but the form stays around - why it this?

Charlie
 
Charlie,
sub button_onclick

dim f as new form2
f.show

end sub


then I thought that at the 'end sub' the variable (or object reference)
'f'
fell
out of scope and so the form should be garbage collected

but the form stays around - why it this?

It goes out of scope for your program, however it is still active, as long
as you don't close it, than the garbage collector will not release it.

That is with all things that you declare by the way. That it goes out of
scope does not mean that it will be released if there is still whatever
reference to or from it.

That is exactly what the GC has to investigate.

I hope this helps,

Cor
 
CharlieOz1 said:
sub button_onclick

dim f as new form2
f.show

end sub


then I thought that at the 'end sub' the variable (or object reference)
'f'
fell
out of scope and so the form should be garbage collected

but the form stays around - why it this?

Windows Forms internally hold a reference to the form instance. That's why
the form is not destroyed by the garbage collector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Scoping in VB.Net 3
Scope? 3
execute public sub on other form 2
Create new "me" 24
Unhandled Exception in Form Load in EXE but not in IDE 14
loading forms 4
Switching between forms 8
Variable scope and lifetime 1

Back
Top