Constant based on variable?
I'm making a small game in XNA at the moment.
And I want to base the size of an array on my screen's resolution.
I did it like this:
public const int intBoardheight = (GraphicsAdapter.开发者_如何学CDefaultAdapter.CurrentDisplayMode.Height -150) / 10 ;
public const int intBoardwidth = (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 200) / 10;
public bool[,] GameBoard = new bool[intBoardheight,intBoardwidth];
public bool[,] GameBoardUpdate = new bool[intBoardheight, intBoardwidth];
public int[,] GameBoardInt = new int[intBoardheight, intBoardwidth];
But this gives me the error "The expression being assigned to 'Game_Of_Life_2.Game1.intBoardheight' must be constant".
So, how do I base a constant on a variable?
Thanks in advance!
Simon.
EDIT: Thanks guys! Worked very well!
You can't. Make it a public static readonly int
You can not create a constant but you can make it readonly
public readonly int intBoardheight = ...
A readonly
variable can only be assigned at the declaration or in the constructor. After that it is not possible to change.
since it's variable - based on the current resolution when you run your app - you cannot make this a compile time constant but you can make it readonly
.
精彩评论