best way to define global constants

A

Amadelle

Hi all and thanks again in advance,
What is the best way of defining global constants in a C# application? (A
windows application with no windows forms - basically a set of classes).
Would it be a wise idea to create a clsCommonApp and let all other classes
to be derived from that class? and define all constants in that base class?

Any other suggestions are more than welcome. (BTW this is not for one
constant, I have multiple constants which I would want to have access to
through out the code)

Thanks again for your input and your insight,

Amadelle,
 
C

C# Learner

Amadelle said:
What is the best way of defining global constants in a C# application? (A
windows application with no windows forms - basically a set of classes).
Would it be a wise idea to create a clsCommonApp and let all other classes
to be derived from that class? and define all constants in that base class?

Why would there be a need for other classes to derive from it?
Any other suggestions are more than welcome. (BTW this is not for one
constant, I have multiple constants which I would want to have access to
through out the code)

I think that the best practise would be to make a class called, perhaps,
'Constants', which is solely a container for the constants.

e.g.:

/// <summary>
/// Container for global constants.
/// </summary>
public class Constants
{
/// <remarks>
/// Private constructor to prevent instantiation of, or inheritance
// from, this class.
/// </remarks>
private Constants()
{
}

public const int MagicNumber1 = 54532525;
public const int MagicNumber2 = 25242246;
}
 
P

Pete Wright

Statics would be best in that constant class so that you don't force your
class user to instantiate an object in order to get to the constant values.
 
B

Bob Powell [MVP]

I very often use the enum like this...

public enum LVMDefs

{

LVM_FIRST =0x1000, // ListView messages

LVM_GETITEMRECT =(LVM_FIRST + 14),

LVM_GETSUBITEMRECT =(LVM_FIRST + 56)

}

I've transposed most of the values from WinUser.h into enums like this for
use in interop. Then any class can use LVMDefs.LVM_GETITEMRECT as a
constant.

The purists will probably moan because there is a constant keyword so for
global constants you could use...

class MyConstants

{

public const int MyValue=100;

}

and then reference it as int x=MyConstants.MyValue;

but it's more typing so I took the lazy way out :).

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41
 
C

C# Learner

Pete said:
Statics would be best in that constant class so that you don't force your
class user to instantiate an object in order to get to the constant values.

In the example, there would be no need for the user to instantiate the
class since it uses constant members, which are considered static
members. This is why constant members can't be declared as static -- it
would be redundant.

"Even though constants are considered static members, a
constant-declaration neither requires nor allows a static modifier. It
is an error for the same modifier to appear multiple times in a
constant declaration."

http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_10_3.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top