学习 C# 数组问题(数组数据消失)

发布于 2024-09-28 08:22:09 字数 1269 浏览 0 评论 0原文

这可能是一个范围问题,但以下代码会失效。我从 Cell 和 Map 这两个类构建了一个多维数组。该地图是 X x Y 大小的单元格网格。到目前为止很正常(当我学习一门新语言时,我不断重写这个相同的程序)。为了简洁起见,我将仅发布课程和反映错误的基本测试。当我去打印地图时,我在构造函数期间初始化的整个网格数组在我去打印时消失(空异常,因为网格最终以某种方式为空......)

//misc using up here

namespace Mapper {
class Program {

static void Main(string[] args)
{ //TODO Parser

 int max_x=2;
 int max_y=2;

 Map myMap = new Map(max_x,max_y);
 myMap.print();


}

class Cell
{
 public char type='o';
 public Cell(char inittype){
 this.type=inittype;
 }

 public void printCell(){
 Console.Write(this.type); }

 public void set(char value){
 this.type = value; }
}

class Map
{
 private int max_X; //global
 private int max_Y; //global
 public Cell[,] grid; //global

 public Map(int maxX, int maxY) {
 Cell[,] grid = new Cell[maxX, MaxY];
 this.max_X = maxX; //Store constructor provided dimensions for global use
 this.max_Y = maxY; 
 for(int yv=0; yv &lt max_Y; yv++){
  for(int xv=0, xv &lt max_X;xv++){
    grid[xv,yx] = new Cell('x');
  }
 }

 public void print() {
 for(int yv=0; yv &lt max_Y; yv++){
  for(int xv=0, xv &lt max_X;xv++){
    grid[xv,yx].printCell();
  }
 }

}}

运行跟踪一切看起来都很好,直到 Map myMap 行完成..换句话说,构造函数似乎没有“粘住”,我最终得到一个空网格(它们都为空。)我只能假设这是某种程度上的范围问题......我错过了什么。 ...?我是否破坏了构造函数?

This is likely a scoping issue but the following code dies. I build a multi-dimensional array from 2 classes, Cell and Map. The map is an X by Y sized grid of Cells. Pretty normal so far (I keep re-writing this same program when I learn a new language). For brevity I'll just post the classes and a basic test that reflects the error. When I go to print the map the whole grid array I initalized during the construtor vanishes when I go to print (Null exception since grid ends up empty some how...)

//misc using up here

namespace Mapper {
class Program {

static void Main(string[] args)
{ //TODO Parser

 int max_x=2;
 int max_y=2;

 Map myMap = new Map(max_x,max_y);
 myMap.print();


}

class Cell
{
 public char type='o';
 public Cell(char inittype){
 this.type=inittype;
 }

 public void printCell(){
 Console.Write(this.type); }

 public void set(char value){
 this.type = value; }
}

class Map
{
 private int max_X; //global
 private int max_Y; //global
 public Cell[,] grid; //global

 public Map(int maxX, int maxY) {
 Cell[,] grid = new Cell[maxX, MaxY];
 this.max_X = maxX; //Store constructor provided dimensions for global use
 this.max_Y = maxY; 
 for(int yv=0; yv < max_Y; yv++){
  for(int xv=0, xv < max_X;xv++){
    grid[xv,yx] = new Cell('x');
  }
 }

 public void print() {
 for(int yv=0; yv < max_Y; yv++){
  for(int xv=0, xv < max_X;xv++){
    grid[xv,yx].printCell();
  }
 }

}}

Running a trace everything looks fine until the Map myMap line completes... in other words it seems like the constructor doesn't "stick" and I get an empty grid in the end (they all go null.) I can only assume it's a scope issue in some way... what am I missing....? Did I bork the constructor?

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

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

发布评论

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

评论(3

泪是无色的血 2024-10-05 08:22:09

问题就在这里:

public Cell[,] grid; //global

public Map(int maxX, int maxY) {
    Cell[,] grid = new Cell[maxX, MaxY];
    ...

您已经声明了一个名为 grid 的实例成员和一个名为 grid 的局部变量,但您只更新了局部变量。

要修复此问题,请将上面提到的最后一行更改为:

grid = new Cell[maxX, maxY];

您的代码中还存在大量编译错误 - 问题中的代码不可能是您正在运行的代码。下次请使用复制粘贴将代码复制到问题中。

此外,注释 //global 具有误导性。实例成员不是全局变量。 C# 中与全局变量最接近的等效项是静态成员。

The problem is here:

public Cell[,] grid; //global

public Map(int maxX, int maxY) {
    Cell[,] grid = new Cell[maxX, MaxY];
    ...

You've declared an instance member called grid and a local variable called grid, but you're only updating the local variable.

To fix it, change the last line mentioned above to this:

grid = new Cell[maxX, maxY];

You also have a large number of compile errors in your code - the code in the question could not possibly be the code you are running. Next time please use copy and paste to copy the code into the question.

Also the comment //global is misleading. An instance member is not a global variable. The closest equivalent to a global variable in C# is a static member.

幻想少年梦 2024-10-05 08:22:09

在构造函数中,您分配给本地网格变量,而不是类网格变量。

Cell[,] grid = new Cell[maxX, MaxY];

应该是

this.grid = new Cell[maxX, maxY];

In your constructor, you're assigning to a local grid variable, not the class grid variable.

Cell[,] grid = new Cell[maxX, MaxY];

should be

this.grid = new Cell[maxX, maxY];
凑诗 2024-10-05 08:22:09

在构造函数中,您声明了 Cell[,] 网格,它隐藏了类级别的 Cell[,] 网格。
:)

In the constructor, you're declaring Cell[,] grid, which is hiding the class-level Cell[,] grid.
:)

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