Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
2.80/5 (2 votes)
See more:
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 :
C#
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 :

C#
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 ..
Posted
Updated 3-Jun-16 10:48am
v2

Hi, this is simple.
C#
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.
 
Share this answer
 
Comments
Gun Gun Febrianza 3-Jun-16 5:08am    
now this is obvious, thanks for helping me :)

another way is :

private void button1_Click(object sender, EventArgs e)
{
Class1 sample = new Class1(this);
sample.send();
}
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".
 
Share this answer
 
Comments
Gun Gun Febrianza 3-Jun-16 5:07am    
Bookmark your articles and i am reading now :)
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
 
Share this answer
 
Comments
Gun Gun Febrianza 4-Jun-16 8:28am    
vote five start for your effort sir (y)
bookmarked your articles . :)
Sergey Alexandrovich Kryukov 4-Jun-16 9:52am    
Thank you.
Will you accept the answer formally then?
—SA
Gun Gun Febrianza 5-Jun-16 3:16am    
accepted ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900