int count,x,y;        
        
        count = 1;  //此变量数量根据需要随时改变,姑且默认为1吧
        x = 8;  y = 5;
      
        public class TRtGraphic
        {
            public Panel ChlPanel = new Panel();
            public Panel[,] CellSub = new Panel[x, y];
        }
        public static TRtGraphic[] RtGraphic = new TRtGraphic[count];
       RtGraphic = new TRtGraphic[count ] ;       for (int P = 0; P < count ; P++)
       {
           RtGraphic[P].ChlPanel = new Panel();                RtGraphic[P].ChlPanel = new Panel();
                RtGraphic[P].ChlPanel.Width  = 800;
                RtGraphic[P].ChlPanel.Height = 600;
                RtGraphic[P].ChlPanel.Left   = 8;
                RtGraphic[P].ChlPanel.Top    = 8;                for (int i = 0; i < 8; i++)
                    for (int j = 0; j < 5; j++)
                    {
                     RtGraphic[P].CellSub [i, j] = new Panel();
                     RtGraphic[P].CellSub[i, j].Parent = RtGraphic[P].ChlPanel;
                    }
       }一运行到RtGraphic[P].ChlPanel = new Panel();
这就出错.如果把class换成struct连编译都不能通过.

解决方案 »

  1.   

     int count,x,y;        
            
            count = 1;  //此变量数量根据需要随时改变,姑且默认为1吧 
            x = 8;  y = 5; 
          
            public class TRtGraphic 
            { 
                public Panel ChlPanel = new Panel(); 
                public Panel[,] CellSub = new Panel[x, y]; 
            } 
            public static TRtGraphic[] RtGraphic = new TRtGraphic[count];       for(int i =0;i<count;i++)
          { 
            数组还没有赋值, 在下面就不能访问, 
            RtGraphic[i] = new TRtGraphic[count] ; 
          }
          for (int P = 0; P < count ; P++) 
          { 
              RtGraphic[P].ChlPanel = new Panel();                 RtGraphic[P].ChlPanel = new Panel(); 
                    RtGraphic[P].ChlPanel.Width  = 800; 
                    RtGraphic[P].ChlPanel.Height = 600; 
                    RtGraphic[P].ChlPanel.Left  = 8; 
                    RtGraphic[P].ChlPanel.Top    = 8;                 for (int i = 0; i < 8; i++) 
                        for (int j = 0; j < 5; j++) 
                        { 
                        RtGraphic[P].CellSub [i, j] = new Panel(); 
                        RtGraphic[P].CellSub[i, j].Parent = RtGraphic[P].ChlPanel; 
                        } 
          } 
     
      

  2.   

    int count,x,y;        
            
            count = 1;  //此变量数量根据需要随时改变,姑且默认为1吧 
            x = 8;  y = 5; 
          
            public class TRtGraphic 
            { 
                public Panel ChlPanel = new Panel(); 
                public Panel[,] CellSub = new Panel[x, y]; 
            } 
            public static TRtGraphic[] RtGraphic = new TRtGraphic[count];       for(int i =0;i <count;i++) 
          { 
            数组还没有赋值, 在下面就不能访问, 
            RtGraphic[i] = new TRtGraphic() ; 
          } 
          for (int P = 0; P < count ; P++) 
          { 
              RtGraphic[P].ChlPanel = new Panel();                 RtGraphic[P].ChlPanel = new Panel(); 
                    RtGraphic[P].ChlPanel.Width  = 800; 
                    RtGraphic[P].ChlPanel.Height = 600; 
                    RtGraphic[P].ChlPanel.Left  = 8; 
                    RtGraphic[P].ChlPanel.Top    = 8;                 for (int i = 0; i < 8; i++) 
                        for (int j = 0; j < 5; j++) 
                        { 
                        RtGraphic[P].CellSub [i, j] = new Panel(); 
                        RtGraphic[P].CellSub[i, j].Parent = RtGraphic[P].ChlPanel; 
                        } 
          } 
      

  3.   

    public class TRtGraphic 
    public struct TRtGraphic 我觉得用struct 比较简单,内存占用也比较小,可是上面的方式如果换成struct 
    这种表达式public Panel[,] CellSub = new Panel[x, y];  就会出错.有没有更好的办法处理struct 中的字段?(如果处理一般的可变数组没问题,但只有加入panel或者label组件的声明,就会出错)