How to store common constants

A

Alex

Hello everybody,

here is the situation: in C++ if I had to use some constant common for
all classes from the application, normally I would've created common.h
file, added line something like

//in common.h
const int COMMON_CONST = 1;

and after this included this common.h in stdafx.h:

//in stdafx.h
#include "common.h"

Now any C++ class could use this COMMON_CONST.

It appears I cannot put something like #region Consts directly under
my application namespace. What is the way to store some constants,
which can be used by all classes from the application in C#?

Thanks,
Alex
 
T

tat

Hello everybody,

here is the situation: in C++ if I had to use some constant common for
all classes from the application, normally I would've created common.h
file, added line something like

//in common.h
const int       COMMON_CONST  =  1;

and after this included this common.h in stdafx.h:

//in stdafx.h
#include "common.h"

Now any C++ class could use this COMMON_CONST.

It appears I cannot put something like #region Consts directly under
my application namespace. What is the way to store some constants,
which can be used by all classes from the application in C#?

Thanks,
Alex
Unlike C++, you can't declare global constant under a namespace. You
have to put it inside a class.

Try this
public class ClassOfConstants
{
public const int CommonConstant = 1; // this is like public static
const int CommonConstant = 1; in c++
}

When you call it
.....
int x = ClassOfConstants.CommonConstant;
If this class of constants is in different namespace, you will need to
do something like
int x = SomeNamepace.ClassOfConstants.CommonConstant;

Hope that it answers your question.
Cheers
 
A

Alex

One thing though, I cannot declare in class let's say ClassOfConstants

public const int CommonConstant = 1;

and then use it like

int a = ClassOfConstants.CommonConst;

I'm getting an error. I'm going to declare this constant as a regular
variable with pre-defined value and later on will try not to change
it :)

//class ClassOfConstants
public int CommonConstant = 1

int a = ClassOfConstants.CommonConstant; //works OK
 
H

Hans Kesting

After serious thinking Alex wrote :
One thing though, I cannot declare in class let's say ClassOfConstants

public const int CommonConstant = 1;

and then use it like

int a = ClassOfConstants.CommonConst;

I'm getting an error.

It would help if you quoted that error.

It seems you have a typo (but that might be just in your post):
CommonConstant <-> CommonConst


Hans Kesting
 
A

Alex

I cannot understand why some people felt so offended, but here is the
suggested code and error:

public class mml_const : Object
{
public const string REG_LAST_SETTINGS = “abdc”;
}

public partial class MainDlg : Form
{
protected mml_const gconst;

public MainDlg()
{
gconst = new mml_const();
string strTest = gconst.REG_LAST_SETTINGS;

//While compiling here comes an error: error CS0176:
Static
// member 'MyApplication.mml_const.REG_LAST_SETTINGS'
cannot be
// accessed with an instance reference; qualify it with a
type name
// instead
}
}

And I’d expressed my gratitude to tat, because I was able to use his
idea with some changes.

Have a good day,
Alex
 
H

Hans Kesting

Alex brought next idea :
I cannot understand why some people felt so offended, but here is the
suggested code and error:

public class mml_const : Object
{
public const string REG_LAST_SETTINGS = “abdcâ€;
}

public partial class MainDlg : Form
{
protected mml_const gconst;

public MainDlg()
{
gconst = new mml_const();
string strTest = gconst.REG_LAST_SETTINGS;

//While compiling here comes an error: error CS0176:
Static
// member 'MyApplication.mml_const.REG_LAST_SETTINGS'
cannot be
// accessed with an instance reference; qualify it with a
type name
// instead
}
}

The error message says it all:
use
mml_const.REG_LAST_SETTINGS
instead. No need to make an instance.

Hans Kesting
 
H

Hans Kesting

Alex formulated the question :
public class mml_const : Object
{
public const string REG_LAST_SETTINGS = “abdcâ€;
}

By the way: you don't need to explicitly inherit from Object (but it's
not an error).

public class mml_const
{
public const string REG_LAST_SETTINGS = “abdcâ€;
}

is exactly the same.

And you could make this a "static" class, where no instances are
allowed:
public static class mml_const {..}


Hans Kesting
 

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