Constants

  • Thread starter Thread starter Publicjoe
  • Start date Start date
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
 
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.
 
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;
}
 
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;
}
 
Back
Top