473,378 Members | 1,142 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1129
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: -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.