Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Global Variable

Posted on 2004-09-25
9
Medium Priority
?
278 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 340 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 160 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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