简体   繁体   English

对于在C#中使用类的菜鸟需要帮助

[英]Need help for a noob on using classes in C#

I'm working on a program that feeds a number (int) to a 2D array. 我正在开发一个将数字(int)馈入2D数组的程序。

That part is easy, but my professor added that the program should be able to determine where the number was added from, via string (usually, it's a letter). 这部分很简单,但是我的教授补充说,该程序应该能够确定通过字符串(通常是字母)从何处添加数字。

Normally, I simply would've converted the int 2D array into a string one, concatenated the string to the number, and say job is done. 通常,我只是将int 2D数组转换为字符串一,将字符串连接到数字,然后说工作已经完成。

But the professor gave us an "Oh crap" moment when he mentioned it had to be done using a class. 但是当教授提到必须使用一堂课来完成时,教授给了我们一个“噢,废话”的时刻。

I'm not very knowledgeable in classes, I've only used them ever to pass one variable in one form to another, via the get/set method. 我对类不是很了解,我只使用过它们通过get / set方法将一种变量以一种形式传递给另一种形式。

Basically, what the professor wants is for the Array to contain both the int, and the string, in the same index, something like this: 基本上,教授想要的是Array在同一索引中包含int和字符串,如下所示:

The class: 班级:

class ClassName
{
  public int num;
  public string loc;
}

The main program: 主程序:

public frmSGame()
{
  InitializeComponent();
}

ClassName[,] myArray = new ClassName[9,8];

public frmMain_Load(object sender, EventArgs e)
{
  clearArray();
}

public void clearArray()
{            
  for (int y = 0; y < 8; i++)
  {
    for (int x = 0; x < 9; j++)
    {
      myArray[x, y].num = -1;
      myArray[x, y].loc = "A";
    }
  }
}

And there lies my problem. 这就是我的问题。 Running the program yields me a "NullReferenceException was unhandled - Object reference not set to an instance of an object", at the for int loop in the clearArray() function. 运行该程序会在clearArray()函数中的for int循环中产生“未处理NullReferenceException-未将对象引用设置为对象实例”的情况。

I'm really not sure what I did wrong. 我真的不确定自己做错了什么。 Putting the "ClassName[,] myArray = new ClassName[9,8]" inside the clearArray() function did nothing, and since I need to modify the array in multiple functions, I'm pretty sure putting that in every function would lead to disastrous results. 将“ ClassName [,] myArray = new ClassName [9,8]”放在clearArray()函数中没有任何作用,并且由于我需要在多个函数中修改数组,因此我很确定将其放入每个函数中会导致造成灾难性的结果。

Might I have some help / advice in this, please? 可以给我一些帮助/建议吗?

The declaration ClassName[,] myArray = new ClassName[9,8]; 声明ClassName[,] myArray = new ClassName[9,8]; makes an array with spaces for 72 objects, but it doesn't make 72 objects. 创建一个带有72个对象的空格的数组,但不创建72个对象。 The array is empty to start with. 数组以空开头。

Before you try to do anything with the objects in the array (eg access num or loc ) you need to actually create them. 在尝试对数组中的对象执行任何操作(例如,访问numloc )之前,您需要实际创建它们。 You could do it inside your inner loop here: 您可以在内部循环中执行以下操作:

for (int x = 0; x < 9; j++)
{
  myArray[x, y] = new ClassName();
  myArray[x, y].num = -1;
  myArray[x, y].loc = "A";
}

Since each element of the array is an object, you have to instantiate each element: 由于数组的每个元素都是一个对象,因此您必须实例化每个元素:

for(int x = 0; x < 9; x++)
   for(int y = 0; y < 8; y++)
      myArray[x, y] = new ClassName();

Alternatively, you can simply put that one statement in your existing loop before you set each parameter. 另外,您可以在设置每个参数之前简单地将该语句放入现有循环中。

This class might be a bit easier for you: 这个课程对您来说可能会容易一些:

class ClassName
{
  public int num;
  public string loc;

  public ClassName(int n, string s){
    num = n;
    loc = s;
  }
}

public void clearArray(){
  for(int x = 0; x < 9; x++)
     for(int y = 0; y < 8; y++)
        myArray[x, y] = new ClassName(-1, "A");
}

You can then access these properties by saying: 然后,您可以通过说出这些属性:

myArray[0, 4].num = 19
myArray[0, 4].loc = "NewLocation";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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