Global variable alternative?

T

The Crow

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.
 
N

Newbie

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.
 
M

Marcus Andrén

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.
 
K

Kevin Spencer

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Newbie said:
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,
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 
P

Peter Bromberg [MVP]

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.AppSettings["myConnectionString"].ToString();
 
N

Newbie

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.
 
T

The Crow

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

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The Crow said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top