Global variable in C#

G

G.Esmeijer

Hi,

I would like to use a couple of hundred constant string variable throught my
program. I want to use them from every corner of the software. In Vb I jst
declared the global and it worked . How do I perform this in C#?

p.e.

string NE_01_001 = "Granulator";

How to declare it and where to make it global accessible?

thanks

Regards

gerrit Esmeijer
 
E

Elp

G.Esmeijer said:
Hi,

I would like to use a couple of hundred constant string variable throught my
program. I want to use them from every corner of the software. In Vb I jst
declared the global and it worked . How do I perform this in C#?

Create a new Class. Call it "Constants" for example. Make its construcor
private so that nobody will try to instanciate this class (this is not
mandatory, just to be cleaner). Then add all your constants here like that:

public const string BestSingerEver = "Britney";

You can access them from basically wherever you want in your other classes
just by using: Constants.BestSingerEver
 
J

James Morton

You create a seperate class and declare them as static variables. With const
you do not need to use the static keyword. You can also set these up as
propeties of the class which is a little more efficient code if you want to
mutate the value ever.

Though, I would reccommend at 100plus strings you should look at loading
them into a datatable (though it depends on how your using them).

class MyAppGlobal
{
public const string MyString = "string";

public string stringAsProperty
{
get { return stringAsProperty; }
set { stringAsProperty = value; }
}
}
 
C

Charlie Calvert

G.Esmeijer said:
Hi,

I would like to use a couple of hundred constant string variable throught my
program. I want to use them from every corner of the software. In Vb I jst
declared the global and it worked . How do I perform this in C#?

The idea of global variables is frowned upon in object oriented
programming languages. In cases like this, one solution is to declare
them as members of a class:

class MyClass
{
public const String FOOBAR = "FooBar";
public static String GOOBER = "Goober";
}

The class need not contain only string constants, and can also include
code if it is related to the constants.

In C#, a const variable is implicitly declared as static, so you would
probably prefer the first example, ie FOOBAR.

You must always reference FOOBAR like this:

String MyString = MyClass.FOOBAR;

You can't do this:

MyClass myClass = new MyClass;
String foobar = myClass.FOOBAR;

Static members must be referenced through the type name such as MyClass.
They can't be accessed through a class instance such as myClass. This is
true of both FOOBAR and GOOBER, since they are both static, even though
FOOBAR is made static only implicitly.

The key thing to grasp here is that you are using MyClass as a way of
organizing your code. As a result, you may find that of your hundreds of
String constants, some belong in a class called MyPurchaseOrderData and
another while others belong in a class called MyPayrollData. The point
is to give the classes meaningful names.

By the way, a name like NE_01_001 can, in some cases, be dangerous. You
want to try to give variables meaningful names, such as
NORTH_EAST_SECTOR_01.

- Charlie
 
A

AlexS

Hi, G.Esmeijer

You might consider putting all these hundreds of strings into resources and
use one static function to get them as you need. Depending on how you use
these strings you might find it much more flexible and usable way -
especially if you ever need to translate some into another language.

Take a look at ResourceManager class and related samples

HTH
Alex
 

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