数组不能正确地从一个类传递到另一个类

本文关键字:一个 另一个 正确地 不能 数组 | 更新日期: 2023-09-27 18:03:24

我有两个表单应用程序。一个表单应用程序是计算值并将它们放入数组中。第二种形式的应用程序获取这些值并检查它们,根据这些值绘制一些东西。根据调试器,当涉及到第二种形式时,2 × 2数组为空。

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

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

数组不能正确地从一个类传递到另一个类

在我看来,当你创建一个类Program的实例时,xinteraction也被创建了,但是Main方法没有运行,所以你收到一个空数组。

我的解决方案:

  1. 在类中创建构造函数Program => set xinteracting [0,0] = 1;
  2. 因为数组是静态的,你可以直接从任何地方获取/设置值。只需调用Program.xinteracting[0,0] = 1设置或var value=Program.xinteracting[0,0];获取