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

Global variable alternative?


Hello,

I need to declare a global variable e.g. database connection handle. So that
I hava an access to this variable all over my class. How can I do it in c#?
Do I have to declare it as static ?
Thanks for any help.
Nov 17 '05 #1
9 2737
this is instance global variable example (specific to class instance):

public class A
{
public int MyInt= 42;
}

usage = A a = new A(); int i = a.MyInt;

this is static global variable (accesible from all classes in your
application .)

public class A
{
public static int MyInt= 42;
}

usage = int i = A.MyInt;
if u change public access modifier to private, then whether its static or
instance, you can access it only in your class.
Nov 17 '05 #2
On Thu, 6 Oct 2005 17:18:42 +0800, "Newbie" <d> wrote:

Hello,

I need to declare a global variable e.g. database connection handle. So that
I hava an access to this variable all over my class. How can I do it in c#?
Do I have to declare it as static ?
Thanks for any help.


It depends, static shares a variable between all instances of a class.

Do you have a single instance of your database class that you reuse
for each database call? In that case static isn't needed.

Do you create a new instance of your database class each time you call
the database? In that case you have to use a private static variable.

--
Marcus Andrén

--
Marcus Andrén
Nov 17 '05 #3
Generally speaking, you shouldn't be sharing a database connection. The .Net
platform uses Connection Pooling natively, which means that it is safer to
open and close database connections as quickly as possible, just like files.

If you use the same Connection String for your Connections, they will re-use
a pooled Connection, which is released into the Connection Pool as soon as
you close a Connection.

This was implemented because, as you seem to realize, one of the most
expensive things you can do in a database app is to open a Connection.
Connection Pooling solves this problem without the inherent danger of
leaving a Connection open.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Newbie" <d> wrote in message news:ej**************@TK2MSFTNGP14.phx.gbl...

Hello,

I need to declare a global variable e.g. database connection handle. So
that
I hava an access to this variable all over my class. How can I do it in
c#?
Do I have to declare it as static ?
Thanks for any help.

Nov 17 '05 #4
Hi,

"Newbie" <d> wrote in message news:ej**************@TK2MSFTNGP14.phx.gbl...

Hello,

I need to declare a global variable e.g. database connection handle. So
that
I hava an access to this variable all over my class. How can I do it in
c#?
Do I have to declare it as static ?


Yes, declare it as static in the correct class.

Now as somebody else pointed out, you should not keep a connection open all
the time, unless that the provider you are using does not support pooling
you should open the connection as later as possible and close it as soon as
possible, the framework will take care of optimizing the connections for
you

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 17 '05 #5
Hi,

this is instance global variable example (specific to class instance):

public class A
{
public int MyInt= 42;
}


There is no such a thing, an instance variable's live if dependand of the
instance scope, the thing that it's public is another matter. If you create
more than one instance of A you will have the same qty of MyInt variables.

A GLOBAL variable is a variable that is accesible from anywhere in the
program AND is live during the entire execution. In C# this concept does
not exist , you have a static variable that have the same practical use, but
a little different conceptual meaning.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 17 '05 #6
If it's a database connection string, normally one would declare that as a
key element in the appSettings Section of your configuration file.

<appSettings>
<add key="myConnectionString" value="whatever" />
</appSettings>

You can then access this from anywhere in your app with:

string myConnectionString =
System.Configuration.ConfigurationSettings.AppSett ings["myConnectionString"].ToString();

"Newbie" <d> wrote in message news:ej**************@TK2MSFTNGP14.phx.gbl...

Hello,

I need to declare a global variable e.g. database connection handle. So
that
I hava an access to this variable all over my class. How can I do it in
c#?
Do I have to declare it as static ?
Thanks for any help.

Nov 17 '05 #7
Thanks.
Do you mean the connection object stores the username and password, so it
can re-connect anytime when necessary? On the part of DBMS, you can't
determine the active application, the users connected to the database. If
the client is sort of "connect on demand". And most database takes some time
to connect.

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
Generally speaking, you shouldn't be sharing a database connection. The ..Net platform uses Connection Pooling natively, which means that it is safer to
open and close database connections as quickly as possible, just like files.
If you use the same Connection String for your Connections, they will re-use a pooled Connection, which is released into the Connection Pool as soon as
you close a Connection.

This was implemented because, as you seem to realize, one of the most
expensive things you can do in a database app is to open a Connection.
Connection Pooling solves this problem without the inherent danger of
leaving a Connection open.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

"Newbie" <d> wrote in message

news:ej**************@TK2MSFTNGP14.phx.gbl...

Hello,

I need to declare a global variable e.g. database connection handle. So
that
I hava an access to this variable all over my class. How can I do it in
c#?
Do I have to declare it as static ?
Thanks for any help.


Nov 17 '05 #8
>> this is instance global variable example (specific to class instance):

public class A
{
public int MyInt= 42;
}

yes, this is an instance global variable (its not shared-static or local
variable). and specific to class instance (every instance of A has different
copy of MyInt in the memory). whats wrong with that? i didnt understand you
Nov 17 '05 #9
Hi,
"The Crow" <q> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
this is instance global variable example (specific to class instance):

public class A
{
public int MyInt= 42;
}

yes, this is an instance global variable (its not shared-static or local
variable). and specific to class instance (every instance of A has
different copy of MyInt in the memory). whats wrong with that? i didnt
understand you


It's not global, it's a public variable of one instance of the type A.
It's not accesible from all over the program, only if you have a reference
to A you can use THAT PARTICULAR variable.

There is nothing like an instance global variable, that itself is a
contradiction.

example:
void method1()
{
A a1 = new A();
a1.Myint = 1;
}

void method1()
{
// I want to access the above value , how I do it? if it's global it
should be accesible FROM EVERYWHERE in the program
int i = ????????

}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #10

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

Similar topics

5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
6
by: Salvani Langosta | last post by:
In an Access 97 database, I use serveral global variables that hold information about the database, for example: gstrFileServer - holds the server root where the database is stored...
8
by: Mike Turco | last post by:
This is a strange one. I have an app that needs to know who is using the program but there's no need for security. When the program opens a form comes up with a listbox. The user double-clicks...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
5
by: Scott Durrett | last post by:
I know and have been told that global variables are "bad". I must say that I agree but what's the alternative to this? 1. I have an application that logs into a website. The site returns me a...
3
by: caldera | last post by:
hi, I want to add a script(window.open script actually) in global.asax, for example Application_BeginRequest. I used Response.Write but opened page locked however main page doesn't. Main page...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.