473,473 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1132
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers...
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
3
by: marco_segurini | last post by:
Hi, I am using VS 2005. If I compile the following code only line 6 returns me an error while line 9 returns a warning. If I comment the line 6 and debug the program the assignments of lines...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
1
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid,...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.