Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Global Variable

Posted on 2004-09-25
9
Medium Priority
?
274 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 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

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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

715 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