Solved

Global Variable

Posted on 2004-09-25
9
236 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now