Solved

Global Variable

Posted on 2004-09-25
9
257 Views
Last Modified: 2010-04-15
Hello In my project namespace I want to define a registry key. The value of this key should be available to all classes in the namespace. In c++ I could do this with

#define REG_KEY (Software\\Key1\\Key2);

How can I do this in C#?

Suppose I wanted to do the same, except this time make REG_KEY accessible to all name spaces in my solution/project. Is this possible?
0
Comment
Question by:auk_ie
  • 5
  • 3
9 Comments
 
LVL 15

Accepted Solution

by:
Timbo87 earned 85 total points
ID: 12153481
No, you can't make a global variable like C++, but you can do something similar.

namespace NamespaceOfYourClasses
{
     public class GlobalVars
     {
          public const string REG_KEY = "Software\\Key1\\Key2";
     }
}

Then you can access it from all the classes in that namespace, or use the fully qualified name if it's not. For example:
Console.WriteLine(GlobalVars.REG_KEY);
0
 
LVL 9

Assisted Solution

by:tinchos
tinchos earned 40 total points
ID: 12153599
I don't quite agree with Timbo's solution

As you don't have global variables you should use static variables instead

so you would end having

namespace NamespaceOfYourClasses
{
     public class GlobalVars
     {
          public static const string REG_KEY = "Software\\Key1\\Key2";
     }
}

and you would use it as Timbo said

Console.WriteLine(NamespaceOfYourClasses.GlobalVars.REG_KEY);

If you want to use Timbo's definition of the class you should do

Console.WriteLine( (new GlobalVars()).REG_KEY);

Hope this helps

Tincho
0
 
LVL 2

Expert Comment

by:fulp02
ID: 12154236
Well I don't quite agree with tinchos
.Net String objects are immutable meaning once they are created they cannot be changed
and using static would limit you to what you can use the object for indexers,destructors and types
so depending on how you would need to use the reg key is up to you.
Otherwise tinchos is correct and so Is Jimbo
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 9

Expert Comment

by:tinchos
ID: 12154695
I know what you're talking about,

but if you want to access a variable without the need to create an object you need to make that variable a static one.

thats why both pieces of code are correct

namespace NamespaceOfYourClasses
{
     public class GlobalVars
     {
          public static const string REG_KEY = "Software\\Key1\\Key2";
     }
}
Console.WriteLine(NamespaceOfYourClasses.GlobalVars.REG_KEY);

and

namespace NamespaceOfYourClasses
{
     public class GlobalVars
     {
          public const string REG_KEY = "Software\\Key1\\Key2";
     }
}
Console.WriteLine( (new GlobalVars()).REG_KEY);

Tincho
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12155198
A string that has the const modifier cannot have the static modifer. The compiler will throw a compile time error.

public static const string REG_KEY = "Software\\Key1\\Key2";
The constant 'MyNamespace.GlobalVars.REG_KEY' cannot be marked static

In my example, GlobalVar does NOT need to be instantiated. You can try it yourself. I just assumed he wanted a single, constant, string accross the whole app. If he wants to change it he can change the code to:
public static string REG_KEY = "Software\\Key1\\Key2";
0
 
LVL 9

Expert Comment

by:tinchos
ID: 12155358
Sorry Timbo, but as far as I know all variables that are not declared as static need an instantiated object to be called for

Tincho
0
 
LVL 9

Expert Comment

by:tinchos
ID: 12155360
you're right about the static const the right way would be as you said

public static string REG_KEY = "Software\\Key1\\Key2";

Tincho
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12155478
From Inside C# 2nd Edition by Tom Archer:
"Notice that there's no need for the client to instantiate the class because, by default, const members are static."
0
 
LVL 9

Expert Comment

by:tinchos
ID: 12155563
Ok, if that's so, I guess I owe you an apologize

Sorry about that one.

Tincho
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question