473,395 Members | 1,516 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,395 software developers and data experts.

global vs local

JJ
Hi All,

I was wondering if I create an object in public main
does it persist for the lifetime of the app? If not where
do u create objects that will persist for the lifetime of
app so that when I call a method in a class I can
reference that object? By the way I am creating a win
form app in c#.

Thanks,

JJ
Nov 15 '05 #1
3 1987
Managed apps (ie, garbage collected) take some adjustments in thinking. You
can create a "local" object and pass a reference to it to another variable
(variable B), and even when the original reference goes out of scope, the
object will remain accessible thru variable B. Therefore, it will not go out
of scope nor be garbage collected as long as there remains any valid
reference to it anywhere in your app. Reference objects are all created on
the managed heap.

Mountain

"JJ" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Hi All,

I was wondering if I create an object in public main
does it persist for the lifetime of the app? If not where
do u create objects that will persist for the lifetime of
app so that when I call a method in a class I can
reference that object? By the way I am creating a win
form app in c#.

Thanks,

JJ

Nov 15 '05 #2
JJ,

This depends. If it is a local variable, then it might not. If the
variable is not passed to anything and not referenced further in the
procedure, then the CLR might determine that it is not accessible and make
it eligible for garbage collection.

However, this is digressing. For what you want to do, you probably want
to expose whatever value/property/object you want using a static property.
You can make a static property/field and that will last for the lifetime of
the application.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JJ" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Hi All,

I was wondering if I create an object in public main
does it persist for the lifetime of the app? If not where
do u create objects that will persist for the lifetime of
app so that when I call a method in a class I can
reference that object? By the way I am creating a win
form app in c#.

Thanks,

JJ

Nov 15 '05 #3
JJ
Thanks Guys,

I think I understand. So if I create, let's say a
XPathNavigator object to a XmlPathDocument and I want to
reference it in another procedure. One I can put it in
the method signature a (XPathNavigator nav1) and ref that
way or two I can create a static XpathNavigator varaible,
XPathNavigator nav2 in class global and then assign it in
the initial procedure such as
XPathNavigator nav = xd.CreateNavigator();
nav2 = nav;

Correct?

If so what is the proper coding way of doing things
meaning which method is preferable?

Thanks,
JJ

-----Original Message-----
Managed apps (ie, garbage collected) take some adjustments in thinking. Youcan create a "local" object and pass a reference to it to another variable(variable B), and even when the original reference goes out of scope, theobject will remain accessible thru variable B. Therefore, it will not go outof scope nor be garbage collected as long as there remains any validreference to it anywhere in your app. Reference objects are all created onthe managed heap.

Mountain

"JJ" <an*******@discussions.microsoft.com> wrote in messagenews:02****************************@phx.gbl...
Hi All,

I was wondering if I create an object in public main
does it persist for the lifetime of the app? If not where do u create objects that will persist for the lifetime of app so that when I call a method in a class I can
reference that object? By the way I am creating a win
form app in c#.

Thanks,

JJ

.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
3
by: Kyle Root | last post by:
I'm writing a little program that compares two versions and tells you whether you are upgrading, downgrading, or it's the same version. Unfortunately, I haven't gotten very far, in fact I'm at a...
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
11
by: tuom.larsen | last post by:
Dear list, I'm writing very simple state machine library, like this: _state = None def set_state(state): global _state _state = state
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.