无法访问form2中的公共静态类 [英] Could not reach public static class in form2

查看:75
本文介绍了无法访问form2中的公共静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i有2个表单,form1在class里面有classe,我在form1中生成那些类的公共静态数组。

static public father [] xx = new father [3]; //< ----见下面代码中的A行

在form2中我写了

int saveIn;

saveIn = Form1.xx [0] .P [0] .V [0] .X;

progrm没有运行错误是

不一致的可访问性:字段类型'test.Form1.father []'比字段'test.Form1.xx'更难以访问



我怎么能重新定义上面的类才能拥有该程序很多,谢谢



我尝试过:



 //类在form1 
类MyNum
{
private int x;
public MyNum(){}
public MyNum(int Xx){X = Xx; }
public int X {get {return x; } set {x = value; }
} // MyNum
类Sun
{
public int n;
public MyNum [] V;
public Sun(){}
public Sun(int Xn){N = Xn; }
public int N {get {return n; } set {n = value;
public MyNum this [int i]
{get {return V [i]; } set {V [i] = value; }
} // MyLine
class father
{
public int x;
public Sun [] P;
public father(){}
public father(int Xx){X = Xx; }
public int X {get {return x; } set {x = value;
public Sun this [int i] {get {return P [i]; } {P [i] = value; }
} //父亲
静态公共父亲[] xx =新父亲[3]; //< --------------行A
//下面的行在Form_2_Load
int SaveInt;
saveInt = Form1.xx [0] .P [0] .V [0] .X;

解决方案

C#区分大小写;这两行不是指同一个变量:

 int SaveInt; 
saveInt = Form1.xx [0] .P [0] .V [0] .X;



修复它,它会编译。

它不起作用 - 你会得到空引用错误,因为你没有在数组 xx 中创建任何实例 - 但它会编译。

你也需要这样的东西:

  for  int  i =  0 ; i <   3 ; i ++)
{
xx [i] = new father();
}


我应该将该类声明为公开类似

公共类父亲


Hello
i have 2 forms ,form1 has classe inside class ,i generate public static array of those classes in form1.
static public father[] xx = new father[3];//<----see line A in the code below
In form2 i wrote
int saveIn;
saveIn = Form1.xx[0].P[0].V[0].X;
the progrm did not run the error is
Inconsistent accessibility: field type 'test.Form1.father[]' is less accessible than field 'test.Form1.xx'

how could i redefine the above classes in order to have the program run ?
many thanks

What I have tried:

//Classes are in form1
    class MyNum
    {
        private int x;
        public MyNum() { }
        public MyNum(int Xx) { X = Xx; }
        public int X { get { return x; } set { x = value; } }
    }//MyNum
    class Sun
    {
        public int n;
        public MyNum[] V;
        public Sun() { }
        public Sun(int Xn) { N = Xn; }
        public int N { get { return n; } set { n = value; } }
        public MyNum this[int i]
        { get { return V[i]; } set { V[i] = value; } }
    }//MyLine
    class father
    {
        public int x;
        public Sun[] P;
        public father() { }
        public father(int Xx) { X = Xx; }
        public int X { get { return x; } set { x = value; } }
        public Sun this[int i] { get { return P[i]; } set { P[i] = value; } }
    }//father
    static public father[] xx = new father[3];//<--------------Line A
    // the line below is in Form_2_Load
         int SaveInt;
     saveInt = Form1.xx[0].P[0].V[0].X;

解决方案

C# is case sensitive; these two lines do not refer to the same variable:

int SaveInt;
saveInt = Form1.xx[0].P[0].V[0].X;


Fix that, and it'll compile.
It won't work - you'll get null reference errors because you haven't created any father instances in the array xx - but it'll compile.
You need to something like this as well:

for (int i = 0; i < 3; i++)
   {
   xx[i] = new father();
   }


i should declare the class as public like

public class father


这篇关于无法访问form2中的公共静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆