传递参考文献

发布于 2024-10-27 17:09:41 字数 3214 浏览 1 评论 0原文

我想知道一种在班级之间传递参考的好方法。

基本上我的检查代码中有两个类:

一个是检查器游戏代码..

        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

另一个代码是表单代码:

    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
    }

问题是这不起作用..当我使用 gameRef 引用时,它会抛出一个空点异常,例如 gameRef.piecePromotion (); // nullerpointException

好吧,我更新了我的问题:

如果我将对象引用设为静态,它会起作用:

public static Game gameRef; 

但不会:

 public Game gameRef; 

会发生什么 私人无效a1_Click(对象发送者,EventArgs e) { codeFile.ExecuteAll(rowStart,columnStart,rowEnd,columnEnd); //每次我决定将一块移动到新的图片框时都会执行此操作。 gameRef.piecePromotion();// 接下来执行。 } 由于Game gameRef不是静态的,ExcuteAll方法执行一次后,gameRef就变为null(而在执行完之前就被赋值了)。

这是 ExecuteAll 方法:

 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.

它通过代码以某种方式将 gameRef 重置为 null。所以我检查了整个代码,是否创建了 Checkers 类型的新对象(我的 winform 部分类)..但是单击 ctrl+F ..我只发现了一个创建对象引用的实例。

为什么它将 gameRef 重置为 null,在为其分配对象引用后

,我仅在事件中使用 gameRef...我只实例化 Game 类和 CheckerCode 类一次..并且我不会通过代码杀死引用。我的引用仅在图片框单击事件内激活:

        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

        }

我在调试模式下查看了代码..

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

因此,当代码继续运行几个步骤后。在表单类/文件(跳棋)中

       gameRef.piecePromotion(); // gameRef is null

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 e.g. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝咒 2024-11-03 17:09:41

看起来在某个时刻,要么 gameRef 在设置之前被使用,要么被设置为 null。

由于类没有一个就没有任何意义,我总是会拥有类不变量的一部分(一组必须始终为真的条件),并在构造函数中强制执行这一点:

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();
}

我也很好奇你为什么要明确地谈论关于“参考文献”。虽然正确,但由于对象始终是引用,因此在 C# 语言中谈论它们是不常见的。这让我想知道 Game 是否可能是一个结构,在这种情况下其他事情会出错(您可以保存对结构的引用,但不值得维护它所需的额外工作)。

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).

甜味超标? 2024-11-03 17:09:41

默认情况下,.NET 将通过引用传递引用类型。您确定 PricePromotion 对象中没有空指针吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文