Solved

Global Variable

Posted on 2004-09-25
9
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

615 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