Constants

P

Publicjoe

OK, my brain has turned to mush on this one.

I want to implement some constant integers that can be used by several
classes in the same namespace. i.e.

namespace MySpace
{
const int THEBOXWIDTH = 24;
const int THEBOXHEIGHT = 48;

class A
{
private int theHeight = THEBOXHEIGHT;
}

class B
{
private int theHeight = THEBOXHEIGHT;
}
}

How can I achieve this? Am I going to have to use enums?

Thanks in advance

Mike
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Publicjoe said:
OK, my brain has turned to mush on this one.

I want to implement some constant integers that can be used by several
classes in the same namespace. i.e.

namespace MySpace
{
const int THEBOXWIDTH = 24;
const int THEBOXHEIGHT = 48;
<snip>

Constants must be placed inside a class.
 
T

Tim Wilson

You could just wrap them in another class.

internal class Constants
{
public const int THEBOXWIDTH = 24;
public const int THEBOXHEIGHT = 48;
}

public class A
{
private int theHeight = Constants.THEBOXHEIGHT;
}

public class B
{
private int theHeight = Constants.THEBOXHEIGHT;
}
 
P

Publicjoe

Thanks, works a treat.

Mike

Tim Wilson said:
You could just wrap them in another class.

internal class Constants
{
public const int THEBOXWIDTH = 24;
public const int THEBOXHEIGHT = 48;
}

public class A
{
private int theHeight = Constants.THEBOXHEIGHT;
}

public class B
{
private int theHeight = Constants.THEBOXHEIGHT;
}
 

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