简体   繁体   English

如何使用另一种形式实例化的类中的方法?

[英]How can I use method from an class instantiated in another form?

I have an class instaciaded in the Form1 and I want call it Form2 it is possible? 我在Form1有一个instaciaded类,我想把它称为Form2它有可能吗?

For example: 例如:

public TheClass thClass; //member from Form1

/* ... */ 

public void foo() { 
       thClass = new TheClass(...);
} 

Form 2: 表格2:

public void baa() { 
        Form1 form1 = new Form1();
        form1.thClass.MethodName( .. ) ;
}

I'm getting the following erro when call the .baa() method in Form2 : 我在Form2调用.baa()方法时得到以下.baa()

Object reference not set to an instance of an object.

What's the best way to do this? 最好的方法是什么? ref ? ref I'm wanting do this not instantiated the thClass again. 我想这样做不会再次实例化thClass。

I hope this is clear. 我希望这很清楚。 Thanks in advance. 提前致谢。

You are getting this exception because thClass is null - in your example you have to call foo() before using thClass . 你得到这个异常因为thClassnull - 在你的例子中你必须在使用thClass之前调用foo()

Reference type fields are nothing special - they can be accessed like any other public field of a class (and Form1 is a class). 引用类型字段没有什么特别之处 - 它们可以像类的任何其他公共字段一样被访问(并且Form1是一个类)。 In general you will want to use a property instead though and avoid public fields since any change to them will break existing consumers - a property that can only be set by the owning class you could express as 一般情况下,你会希望使用属性而避免使用公共字段,因为对它们的任何更改都会破坏现有的使用者 - 这个属性只能由你可以表达为的拥有类来设置

public TheClass SomeClass {get; private set;}

you should call Foo() to initizalize "thClass".... 你应该调用Foo()来启动“thClass”....

public void baa() { 
    Form1 form1 = new Form1();
    form1.foo();
    form1.thClass.MethodName( .. ) ;
}

since you initialize the thClass in the foo method, you just invoke the method before you use thClass 因为你在foo方法中初始化thClass,所以你只需在使用thClass之前调用该方法

public void baa() { 
        Form1 form1 = new Form1();
        form1.foo();
        form1.thClass.MethodName( .. ) ;
}

or you can initialize directly 或者你可以直接初始化

public void baa() { 
        Form1 form1 = new Form1();
        form1.thClass = new TheClass(...);
        form1.thClass.MethodName( .. ) ;
}

The best way to do it might be to pass the instance of TheClass in Form1 to Form2's constructor. 最好的方法可能是将Form1中的TheClass实例传递给Form2的构造函数。

// Code from Form 1
public partial class Form1 : Form
{
    private TheClass thClass;
    public Form1()
    {
        InitializeComponent();
    }
    public void foo()
    { 
        thClass = new TheClass(...);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2(thClass);
        objForm2.Show();
    }
}

// Code From Form 2
public partial class Form2 : Form
{
    private TheClass thClass;
    public Form2(TheClass thCls)
    {
        thClass = thCls;
        InitializeComponent();
    }

    private void baa(object sender, EventArgs e)
    {
        thClass.MethodName( .. );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM