Welcome. In regards to your question there really is no concept of a true
global object in C#. Everything is scoped inside a class. To achieve the
illusion of a global you can create a static class that holds the object as a
static member. As an example, to correlate with your question, I have a
static Logger class in most of my projects that exposes static methods to log
information to various listeners. The Logger class acts as a global object
since it is static. If you need to store data with your static class then
you can either create the data as static (generally only advisable for truly
global, state data) or use the singleton pattern to create an instance of
your non-static class and then expose the instance through a static method.
As an example the Debug and Trace classes in .NET are static classes that
expose static methods. Thread.CurrentThread is a pseudo-singleton class that
exposes (through a static property) the current thread's instance data.
In C# pre-2.0 you make a class static by using the following model:
public sealed class Logger
{
private Logger ( ) { }
...
public static void LogThis ( ) { }
public static void LogThat ( ) { }
}
Then in code I can:
Logger.LogThis();
Logger.LogThat();
This doesn't truly make the class static it simply prevents anyone from
using it as an instance class. In 2.0 you can apply the static modifier to
the class declaration to make it a static class. Within your static class
(not in 2.0 though) you can have both static and instance members (like
Thread does). A true static class has only static methods and properties
(like Debug).
For a singleton-pattern you would do the following:
public sealed class Logger
{
private Logger ( ) { }
public Logger Current
{
get { return m_Instance; }
}
public void LogThis ( ) { }
public void LogThat ( ) { }
private static Logger m_Instance = new Logger();
}
and then...
Logger.Current.LogThis();
Logger.Current.LogThat();
The above implementation is defacto and handles most of the multithreading
issues involved with singletons and delays the creation of the instance until
it is actually needed.
For constants you can simply expose them as constants off of any class that
is most appropriate. For example in the logger class you might want to
expose a maximum message length so in the Logger class you would add
public const int MaxMessageLength = 255;
You can then...
if (strMsg.Length > Logger.MaxMessageLength) ...
If your constants are unrelated to anything then you can create a separate
static class that holds nothing but the constants. I do this for file
parsers. I store all the constants that I use within a file parser in a
separate file. This keeps the main parser class clean but still gives me the
constants.
Others may have other ideas as well.
Michael Taylor, MCP, MCAD - 6/23/05
"Marty" wrote:
Hi,
I'm new to C#, I used to code in VB.NET. Where is the best place to
declare all my constants and global objects in my C# project to have
them accessible globally?
I have an event logger class that I want its instance to be accessible
from any other classe in the project.
There is also a bunch of constants that I want to be public for the
whole program.
Many thanks,
Marty