471,853 Members | 809 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

Scope?

Hi,

I've got a question about something which actually works, but I'd like to
know why...

If you create a form in a subroutine and "show" it, once the subroutine
finishes, technically the new form should be out of scope. I guess in a
sence it is, because you can never "get ahold of it" again. However the
form is still there. Maybe my question should be, is the following code bad
style and could it lead to some undesirable effects?

Private Sub MySub()
Dim f as new SomeForm()
f.show()
End Sub
Nov 20 '05 #1
5 1082
If you create a form in a subroutine and "show" it, once the subroutine
finishes, technically the new form should be out of scope. I guess in a
sence it is, because you can never "get ahold of it" again. However the
form is still there. Maybe my question should be, is the following code bad
style and could it lead to some undesirable effects?


First of all, scope != lifetime in .NET. An object can still be alive
in memory after it goes out of scope, but before the garbage collector
removes it.

For forms specifically, there's some additional magic behind the
scenes to keep the Form objects alive as long as the window is shown.
So no, your code isn't "bad".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2
Thanks a lot for the response.

I've got a follow up question:

Suppose I have a 'global' collection called MyCollection. I then fill it in
a subroutine like:

Private Sub FillCollection()
Dim i as integer
for i = 1 to 10
Dim SomeObject as new SomeThingOrOther()
MyCollection.add(SomeObject)
next
end sub

The collection never seems to lose the stuff inside of it, but the
declaration of 'SomeObject' in this scope feels weird.

Is there a chance that the garbage collector could eat the contents of my
collection?

Samud
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If you create a form in a subroutine and "show" it, once the subroutine
finishes, technically the new form should be out of scope. I guess in a
sence it is, because you can never "get ahold of it" again. However the
form is still there. Maybe my question should be, is the following code badstyle and could it lead to some undesirable effects?


First of all, scope != lifetime in .NET. An object can still be alive
in memory after it goes out of scope, but before the garbage collector
removes it.

For forms specifically, there's some additional magic behind the
scenes to keep the Form objects alive as long as the window is shown.
So no, your code isn't "bad".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 20 '05 #3
Thanks a lot for the response.

I've got a follow up question:

Suppose I have a 'global' collection called MyCollection. I then fill it in
a subroutine like:

Private Sub FillCollection()
Dim i as integer
for i = 1 to 10
Dim SomeObject as new SomeThingOrOther()
MyCollection.add(SomeObject)
next
end sub

The collection never seems to lose the stuff inside of it, but the
declaration of 'SomeObject' in this scope feels weird.

Is there a chance that the garbage collector could eat the contents of my
collection?

Samud
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If you create a form in a subroutine and "show" it, once the subroutine
finishes, technically the new form should be out of scope. I guess in a
sence it is, because you can never "get ahold of it" again. However the
form is still there. Maybe my question should be, is the following code badstyle and could it lead to some undesirable effects?


First of all, scope != lifetime in .NET. An object can still be alive
in memory after it goes out of scope, but before the garbage collector
removes it.

For forms specifically, there's some additional magic behind the
scenes to keep the Form objects alive as long as the window is shown.
So no, your code isn't "bad".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 20 '05 #4
No, it won't -- the MyCollection object is holding a reference to
SomeObject. As long as the object is referenced by something reachable, it
will not be garbage collected. (If you drop your reference to
MyCollection, then it will no longer be reachable -- and neither will
SomeObject. At this point, they're both candidates for garbage collection)
"Samud" <si****@hotmail.com> wrote in message
news:40***********************@nan-newsreader-01.noos.net...
Thanks a lot for the response.

I've got a follow up question:

Suppose I have a 'global' collection called MyCollection. I then fill it in a subroutine like:

Private Sub FillCollection()
Dim i as integer
for i = 1 to 10
Dim SomeObject as new SomeThingOrOther()
MyCollection.add(SomeObject)
next
end sub

The collection never seems to lose the stuff inside of it, but the
declaration of 'SomeObject' in this scope feels weird.

Is there a chance that the garbage collector could eat the contents of my
collection?

Samud
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If you create a form in a subroutine and "show" it, once the subroutine
finishes, technically the new form should be out of scope. I guess in asence it is, because you can never "get ahold of it" again. However theform is still there. Maybe my question should be, is the following
code
badstyle and could it lead to some undesirable effects?


First of all, scope != lifetime in .NET. An object can still be alive
in memory after it goes out of scope, but before the garbage collector
removes it.

For forms specifically, there's some additional magic behind the
scenes to keep the Form objects alive as long as the window is shown.
So no, your code isn't "bad".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 20 '05 #5
No, it won't -- the MyCollection object is holding a reference to
SomeObject. As long as the object is referenced by something reachable, it
will not be garbage collected. (If you drop your reference to
MyCollection, then it will no longer be reachable -- and neither will
SomeObject. At this point, they're both candidates for garbage collection)
"Samud" <si****@hotmail.com> wrote in message
news:40***********************@nan-newsreader-01.noos.net...
Thanks a lot for the response.

I've got a follow up question:

Suppose I have a 'global' collection called MyCollection. I then fill it in a subroutine like:

Private Sub FillCollection()
Dim i as integer
for i = 1 to 10
Dim SomeObject as new SomeThingOrOther()
MyCollection.add(SomeObject)
next
end sub

The collection never seems to lose the stuff inside of it, but the
declaration of 'SomeObject' in this scope feels weird.

Is there a chance that the garbage collector could eat the contents of my
collection?

Samud
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If you create a form in a subroutine and "show" it, once the subroutine
finishes, technically the new form should be out of scope. I guess in asence it is, because you can never "get ahold of it" again. However theform is still there. Maybe my question should be, is the following
code
badstyle and could it lead to some undesirable effects?


First of all, scope != lifetime in .NET. An object can still be alive
in memory after it goes out of scope, but before the garbage collector
removes it.

For forms specifically, there's some additional magic behind the
scenes to keep the Form objects alive as long as the window is shown.
So no, your code isn't "bad".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anonymous | last post: by
6 posts views Thread by pembed2003 | last post: by
5 posts views Thread by pembed2003 | last post: by
8 posts views Thread by TTroy | last post: by
3 posts views Thread by marco_segurini | last post: by
39 posts views Thread by utab | last post: by
7 posts views Thread by Christian Christmann | last post: by
pbmods
1 post views Thread by pbmods | last post: by
1 post views Thread by Giacomo Catenazzi | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.