如何从form1访问类的方法 [英] How class access method from form1

查看:101
本文介绍了如何从form1访问类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为form1的表单和名为class1的类。

如何从form1访问class1?

我的代码出了什么问题?我可以看到错误我总是在mMainForm.getdata(test)中得到空引用;来自class1



这是我的form1代码:

 命名空间 WindowsFormsApplication1 
{
public partial class Form1:Form
{
private Class1 a;
public Form1()
{
InitializeComponent();
a = new Class1( this );
}

public void getdata( string read)
{
textBox1.Text = read;
}

private void button1_Click( object sender,EventArgs e)
{
Class1 sample = new Class1();
sample.send();
}
}
}





这里是class1,有一个方法发送试图从form1访问方法:



 命名空间 WindowsFormsApplication1 
{

public class Class1
{
private Form1 mMainForm;
public Class1(Form1 mainForm)
{
mMainForm = mainForm;
}

public Class1()
{
}

< span class =code-keyword> public void send()
{
mMainForm.getdata( test);
}

}
}





我尝试过:



trid一些代码但不起作用..

解决方案

这是简单。

  private   void  button1_Click( object  sender,EventArgs e)
{
Class1 sample = new Class1();
sample.send();
}



在上面的代码块中,您正在使用 Class1创建 sample 对象的默认构造函数 new Class1(); ,它不会初始化 mMainForm 对象。取而代之的是,使用对象 a ,因为它已初始化 mMainForm 对象。


一般来说,你没有 - 这是一个穷人从OOP的角度来看,它将两个类锁定在一起。

相反,你应该在Class1中创建一个Form类处理的事件。表单调用它自己的方法,并通过属性r方法将任何结果返回给class1实例。

看看这里:在两种形式之间转移信息,第2部分:儿童到父母 [ ^ ] - Form1是Parent,Class1是Child。


首先,您不是从表单访问表单类,而是从一个类访问另一个类。有类和实例;不要混淆他们。



这一切都归结为一个微不足道但却很受欢迎的形式合作问题。为了回答很多这样的问题,我写了这篇文章,请看: 一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



-SA

i have one form called form1 and class called class1.
How a class1 access method from form1?
What is wrong with my code? i can see the fault i always get null reference in mMainForm.getdata("test"); from class1

This is my form1 code :

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Class1 a;
        public Form1()
        {
            InitializeComponent();
            a = new Class1(this);
        }

        public void getdata(string read)
        {
            textBox1.Text = read;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 sample = new Class1();
            sample.send();
        }
    }
}



And here is the class1, there is a method send that tried to access method from form1 :

namespace WindowsFormsApplication1
{

    public class Class1
    {
        private Form1 mMainForm;
        public Class1(Form1 mainForm)
        {
            mMainForm = mainForm;
        }

        public Class1()
        {
        }

        public void send()
        {
            mMainForm.getdata("test");
        }

    }
}



What I have tried:

trid some code from but not works ..

解决方案

Hi, this is simple.

private void button1_Click(object sender, EventArgs e)
{
    Class1 sample = new Class1();
    sample.send();
}


In this above block of code, you are creating sample object using Class1's default constructor new Class1();, which won't initialize mMainForm object. Instead this, use object a, because it has mMainForm object initialized.


Generally speaking, you don't - it's a poor idea from an OOPs point of view in that it "locks" the two classes together.
Instead, you should create an event in your Class1 which the Form class handles. The form calls it's own method, and returns any results to the class1 instance via a property r method.
Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] - the Form1 is the "Parent", the Class1 is the "Child".


First of all, you access not from form to form class, not from one class to another. There are classes and instances; don't confuse them.

This is all reduced to a trivial but popular problem of form collaboration. To answer many such questions, I wrote this article, please see: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


这篇关于如何从form1访问类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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