C# 从类中调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1157791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:35:48  来源:igfitidea点击:

Calling a method from within a Class

c#classmethodsnullreferenceexception

提问by user

I have 2 Forms (Form1 and Form2) and a class (Class1). Form1 contains a button (Button1) and Form2 contains a RichTextBox (textBox1) When I press Button1 on Form1, I want the method (DoSomethingWithText) to be called. I keep getting "NullReferenceException - Object reference not set to an instance of an object". Here is a code example:

我有 2 个表单(Form1 和 Form2)和一个类(Class1)。Form1 包含一个按钮 (Button1),Form2 包含一个 RichTextBox (textBox1) 当我在 Form1 上按下 Button1 时,我希望调用方法 (DoSomethingWithText)。我不断收到“NullReferenceException - 未将对象引用设置为对象的实例”。这是一个代码示例:

Form1:

表格1:

namespace Test1
{  
    public partial class Form1 : Form  
    {
        Form2 frm2;

        Class1 cl;

        public Form1()  
        { 
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frm2 = new Form2(); 
            cl.DoSomethingWithText();
            frm2.Show()
        } 
   }  
}  

Class1:

类1:

namespace Test1
{
      class Class1
      {
           Test1.Form2 f2;
           public void DoSomethingWithText()
           {
                f2.richTextBox1.Text = "Blah blah blah";
           }
      }
}

How can I call this method from within a class? Any help is greatly appreciated.

如何从类中调用此方法?任何帮助是极大的赞赏。

采纳答案by Ray Booysen

You have to instantiate c1and f2. Try this:

您必须实例化c1f2。尝试这个:

public partial class Form1 : Form  
{
    Form2 frm2;
    Class1 cl;
    public Form1()  
    {  
        c1 = new Class1();
        InitializeComponent();  
    }
    private void button1_Click(object sender, EventArgs e)
    {
      frm2 = new Form2();
      cl.DoSomethingWithText(frm2);
      frm2.Show();
    } 
}

class Class1
{

    public void DoSomethingWithText(Test1.Form2 form)
    {
        form.richTextBox1.Text = "Blah blah blah";
    }
}

UPDATE

更新

As Keith has pointed out, because you're instantiating a new version of Form2, the rich textbox will never show the blah blah blah code. I've updated the sample to fix this.

正如 Keith 指出的那样,因为您正在实例化 的新版本Form2,所以富文本框永远不会显示 blah blah blah 代码。我已经更新了示例来解决这个问题。

回答by Hooloovoo

You need to either declare DoSomethingWithText as a static class or instantiate the reference to Class1.

您需要将 DoSomethingWithText 声明为静态类或实例化对 Class1 的引用。

public static void DoSomethingWithText()           
  {                
    //Code goes here;           
  }

回答by Ed S.

You are never initializing cl (or f2 for that matter).

您永远不会初始化 cl (或 f2 就此而言)。

回答by AdaTheDev

You haven't instantiated an instance of Class1 before you've tried to use it

在尝试使用 Class1 之前,您还没有实例化它

You'd need to do:

你需要做:

private void button1_Click(object sender, EventArgs e)
{
    c1 = new Class1();
    frm2 = new Form2();
    cl.DoSomethingWithText(frm2);
    frm2.Show();
} 

Not I've also added in the passing of frm2 in to the DoSomethingWithText method for it to then use (else you'd end up with another similar exception as f2 hasn't been instantiated in that class.

不是我还添加了将 frm2 传递给 DoSomethingWithText 方法以供使用(否则您最终会遇到另一个类似的异常,因为 f2 尚未在该类中实例化。

回答by Keith

Either instantiate first (see @Ray Booysen's answer) or convert it to a static method:

首先实例化(请参阅@Ray Booysen 的回答)或将其转换为静态方法:

class Class1
{
   public static void DoSomethingWithText( Test1.Form2 f2 )
   {
      f2.richTextBox1.Text = "Blah blah blah";
   }
}

Then:

然后:

 frm2 = new Form2();
 Class1.DoSomethingWithText( frm2 );
 frm2.Show();