Reputation: 11
Im trying to program the reversi game for a school assignment but i have a
problem when im calling an array from another class.
I used this if statement to check if a move is legal. It calls the CheckLegal move method from the playermove class.
if(PlayerMove.CheckLegal(Yarray,Xarray))
{
//changes the turn
if (turn) { turn = false; } else { turn = true; }
if (turn) { value = 1; } else { value = -1; }
GameState[Yarray, Xarray] = value;
Board.Invalidate();
}
The CheckLegal method like this.
public bool CheckLegal(int Yarray,int Xarray)
{
var Form1 = new Form1();
bool result;
if(Convert.ToInt32(Form1.GameState.GetValue(Yarray,Xarray)) == 0) { result = true; }
else { result = false; }
return result;
}
The If statement, if true, changes the value of the Gamestate array at a certain point. The only problem is, is when I call for the Gamestate array in the Checklegal method, I don't get the updated value. But if the CheckLegal method isn't in antother class I do get the updated value.
Can anyone please explain how this works?
And how i can get the updated Gamestate value in my PlayerMove class?
Upvotes: 0
Views: 173
Reputation: 81493
You are creating a new form to check the array, this is bound to fail.
public bool CheckLegal(int Yarray,int Xarray)
{
var Form1 = new Form1();
Its like building a new car to check if you left your wallet in the console. i mean sure you can make a new car, there is no chance your wallet will be there though
Consider making the array static, or passing the array around when you need it
Further Reading
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes.
Passing arrays as arguments (C# Programming Guide)
Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.
Upvotes: 1