数组没有正确地从一个类传递到另一个类

问题描述:

我有2个表单应用程序。一个表单应用程序正在计算值并将其放入数组中。第二个表单应用程序接受这些值并对它们进行检查,根据这些值绘制某些内容。根据调试器,当涉及到第二种形式时,2乘2阵列是空的。数组没有正确地从一个类传递到另一个类

实际上,这两个名称空间在visual studio中的不同文件中,并且使用语句都在第二个窗体中重复使用。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text.RegularExpressions; 

namespace Rextester 
{ 
    public class Program 
    { 
     public const int SIZEX = 656; 
     public const int SIZEY = 656; 
      public static int[,] xinteracting = new int[SIZEX, SIZEY]; //Tells you the points at which it interacts with the different objects 

     public static void Main(string[] args) 
     { 

      xinteracting [0,0] = 1; 
      //Your code goes here 
      //Console.WriteLine("Hello, world!"); 
     } 
    } 
} 
/*Second form window represented from here*/ 
namespace Hello 
{ 

     public class Yollow 
    { 
     public static void Main(string[] args) 
     { 
      Program old = new Program(); 
      int temp = old.xinteracting[0,0]; 
      Console.WriteLine(temp); 
     } 
    } 
} 
+0

首先,由于'xinteracting'是私有的,因此无法编译。如果你解决这个问题,'Program.Main'永远不会从'Yollow.Main'调用,所以'xinteracting [0,0] = 1;'永远不会执行。 – Blorgbeard

+0

@Blorgbeard对不起,我已将私人公开为公开 –

+0

@Blorgbeard刚刚拿起第二件事说,应该从最初的形式的主文件不会自动执行?我的意思是在我真正的程序中,我可以查看xinteracting函数的值,并且在整个表单完成后,我检查这些值,并且它们以我期望的值出现 –

在我看来,当你创建类节目的一个实例,xinteracting创建,太多,但主要方法不跑,这样你会收到一个空数组。

我的解决办法:

  1. 创建类节目构造=>设置xinteracting [0,0] = 1;
  2. 因为数组是静态的,你可以直接从任何地方获取/设置值。只需拨打Program.xinteracting[0,0] = 1var value=Program.xinteracting[0,0];即可获得
+0

我没有那样做吗? 该程序识别old.xinteracting [0,0],所以我敢肯定我正在做的那部分权利,但问题是,它只是没有从一种形式到另一种形式的值。它只是给我一个0,当我调用它之前,我呼吁新的形式值不是0(它是100000) –