C# 传递参考资料
我想知道一个好方法,把一个参考资料从一个班级传递到另一个班级 基本上,我的支票代码中有两个类: 一个是棋盘游戏代码C# 传递参考资料,c#,object,reference,C#,Object,Reference,我想知道一个好方法,把一个参考资料从一个班级传递到另一个班级 基本上,我的支票代码中有两个类: 一个是棋盘游戏代码 class CheckersCode { IchecherMove[,] pieces; public static int counter; Checkers checker; public void ExecuteAll(int columnStart, int rowStart, int
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引用时,它会抛出一个null点例外,例如gameRef.piecePromotion();//空点异常
好的,我更新了我的问题:
如果我将对象引用设置为静态,则它会起作用:
public static Game gameRef;
但不是:
public Game gameRef;
发生的是
私有无效a1_单击(对象发送者,事件参数e)
{
codeFile.ExecuteAll(rowStart,columnStart,rowEnd,columnned);//每次我决定将一个片段移动到新的picturebox时都会执行此操作。
gameRef.piecePromotion();//接下来执行此操作。
}
如果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。因此,我在整个代码中都进行了检查,如果创建了类型检查器的新对象(我的winform部分类)…但是单击ctrl+F..我只找到了一个创建对象引用的实例
在分配对象引用后,为什么要将gameRef重置为null
我只在事件中使用gameRef。。。我只实例化了一次Game类和CheckerCode类。。我不会通过代码删除引用。我的引用仅在picturebox点击事件中激活:
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.
}
因此,当代码在几步之后继续运行时。以类/文件(Checkers)的形式
默认情况下,.NET将按引用传递引用类型。您确定我们的pricePromotion对象中没有空指针吗?在某个点上,它看起来要么在设置前使用了
gameRef
,要么被设置为空
因为没有一个类是没有意义的,所以我将始终拥有类不变量的一部分(必须始终为真的条件集),并在构造函数中强制执行这一点:
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#speak中这样谈论它们是不常见的。这让我想知道游戏是否可能是一个结构,在这种情况下,其他事情都会出错(您可以保留对结构的引用,但不值得为维护它而进行额外的工作)。您的代码还可以。问题一定出在别的地方。请显示您使用gameRef的位置。类
Checkers
是您的主应用程序窗口吗?您是否可能在Checkers类中的某个位置处置或重新分配代码文件实例?代码文件在整个部分类中都被使用。它没有被处理。事件方法包含两个紧挨着另一个codeFile.ExecuteAll和gameRef.piecePromotionPiece升级是游戏对象内部的一个方法。实际上,我第一次成功地传递了一个对象。从调试中可以看到,方法ActainGameReference(Game g)被激活了两次,第二次被设置为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();
}