简体   繁体   中英

Passing References

i want to know a good way to pass a reference from class to class.

basically i have two classes in my check code:

one is a Checker game code..

        class CheckersCode
    {
        IchecherMove[,] pieces;
        public static int counter;
        Checkers checker;
        public void ExecuteAll(int columnStart, int rowStart, int columnEnd, int rowEnd)
        {              
            checker = new Checkers();
            Game g=new Game(pieces, columnStart, rowStart,  columnEnd, rowEnd);// i want this reference to be passed in the method below
            g.MoveValidityManager();
            checker.obtainGameReference(g);//i want this reference to be passed through this method

the other code is the Form code:

    public partial class Checkers : Form
{
    public Checkers()
    {
        InitializeComponent();
    }
    CheckersCode codeFile = new CheckersCode();

    private void Checkers_Load(object sender, EventArgs e)
    {
        chessPics = Pattern();
        PrintPieces(codeFile.FirstLoad());        
    }
    Game gameRef;
    public void obtainGameReference(Game g)// i want that reference to be obtained here
    {
        gameRef=g;and be passed to this
    }

problem is that that doesnt work..when i use the gameRef reference it throws a nuller point exeption eg gameRef.piecePromotion(); // nullerpointException

okay, i updated my question:

it works if i make the object reference static :

public static Game gameRef; 

but not:

 public Game gameRef; 

what happens is private void a1_Click(object sender, EventArgs e) { codeFile.ExecuteAll(rowStart, columnStart, rowEnd, columnEnd);// this is executed each time i decide to move a piece to a new picturebox. gameRef.piecePromotion();// this is executed next. } with the Game gameRef, not being static, after the ExcuteAll method is executed once, the gameRef becomes null (while before it is finishes executed it is assigned).

here is the ExecuteAll method :

 public void ExecuteAll(int columnStart, int rowStart, int columnEnd, int rowEnd)
            {              
                checker = new Checkers();
                Game g=new Game(pieces, columnStart, rowStart,  columnEnd, rowEnd);
                checker.obtainGameReference(g);

                g.MoveValidityManager();// calls a method for a class to be executed. no new instances of Checkers class are being created , inside the game class.

it somehow resets gameRef to null, through the code. So i checked throughout the code, if i have new objects created of type Checkers (my winform partial class)..but clicking ctrl+F ..and i found only one instance where i created an object reference.

why does it reset gameRef to null, after it assigns it the object reference

i use gameRef only inside the events... i only instantiating Game class and CheckerCode class once .. and i dont kill references throught the code . my references are activated inside picturebox click events only:

        private void a2_Click(object sender, EventArgs e)
    {
        if (NextTurn)
        {
            columnEnd = 1;
            rowEnd = 2;
            codeFile.ExecuteAll(rowStart, columnStart, rowEnd, columnEnd);// it does assign the gameRef throughout the method, but at the end....
            gameRef.piecePromotion();// this is reset to null after the above method finishes executing

        }

i looked through the code in a debug mode..

        public void obtainGameReference(Game g)
    {
        gameRef=g;// g is not null, it contains the object. gameRef remains a null.
    }

therefore when the code continues to run a few steps later. in the Form class/File (Checkers)

       gameRef.piecePromotion(); // gameRef is null

It looks like at some point either gameRef is being used before it is set, or it is being set to null.

Since the class makes no sense without one, I would always having one part of the class invariant (the set of conditions that must always be true), and enforce this in the constructor:

private readonly Game _game;//doesn't HAVE to be readonly, but can simplify things if it is
public Checkers(Game game)
{
  if(game == null)
    throw new ArgumentNullException();
  _game = game;
  InitializeComponent();
}

I'm also curious as to why you are talking explicitly about "references". While correct, since objects are always references, it's unusual to talk about them as such in C# speak. This makes me wonder if Game might be a struct, in which case other things are going to go wrong (you can hold a reference to a struct, but it's not worth the extra work needed to maintain it).

By default, .NET will pass reference types by reference. Are you sure there isn't a null pointer in our pricePromotion object?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM