0

how I can call a function located in the Form class from another class?

this is my code.I want call get_data(data) in prog class?

thanks.

public partial class Form1 : Form
{
    get_data(int mydata)
    {
        //code
    }

    //code
    prog var1=new prog();
    var1.start_data();
}



public class prog
{
    public void start_data()
    {
        Thread ct=new Thread(do);
        ct.start();
    }

    private void do()
    {
        int data=40;
        get_data(data);  ///?????????????this is wrog
    }
}
sm k
  • 37
  • 1
  • 8
  • [Here is a great answer to this](http://stackoverflow.com/questions/16226444/how-to-make-method-call-another-one-in-classes-c) – Abel Masila Apr 19 '17 at 09:14
  • You'd better not call Form methods on other threads than the gui thread. In this case there isn't any issue, aslong as you do not access controls in the `get_data()` – Jeroen van Langen Apr 19 '17 at 09:14
  • Best idea is to pass reference of main Form when you creating another class. Than use it like `myMainForm.get_data(data);` – Logman Apr 19 '17 at 09:15
  • what do you intend to do in `get_data` ? do you want to return a value? or is it `void` ? – Mong Zhu Apr 19 '17 at 09:19
  • `get_data(int mydata)` needs a return type, up to now it has the form of a private constructor, which will not even compile... – Mong Zhu Apr 19 '17 at 09:36
  • 1
    Possible duplicate of [How to access form methods and controls from a class in C#?](http://stackoverflow.com/questions/217389/how-to-access-form-methods-and-controls-from-a-class-in-c) – Fabjan Apr 19 '17 at 09:51

3 Answers3

4

If you need to access the current instance of your main form, you could pass it along to the class:

public partial class Form1 : Form
{
    internal void get_data(int mydata)//Change to internal or public, as default is private
    {
        //code
    }

    private void button1_Click(object sender, EventArgs e)
    {
        prog var1 = new prog();
        var1.start_data(this);//pass along instance of your main form
    }
}

public class prog
{
    private Form1 MainForm;
    public void start_data(Form1 form)
    {
        MainForm = form;//set form
        Thread ct = new Thread(doSmt);
        ct.Start();
    }

    private void doSmt()
    {
        int data = 40;
        MainForm.get_data(data);  //use form
    }
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
1

Before you can call a method from another class you need an instance of this class. One way would be to create a new instance.

But I guess you'll have an instance of this class already because it's a form an I guess you are opening this form anywhere; just use this instance.

A hint: I would not put any business logic in my form. I would put it in another class.

private void do()
{
    int data=40;
    Form1 form = new Form1();
    form.get_data(data); 
}

Don't forget to make the get_data method public because you can't access it in another class when it's private.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • 1
    @smk since you have not described what this method is supposed to do, we cannot guess what it means : "dont work correctly". We cannot read minds – Mong Zhu Apr 19 '17 at 09:20
  • @smk are you accessing any variables from the class `Form1` in the `get_data` method? what does it exactly mean that it is not working? – Mong Zhu Apr 19 '17 at 09:35
1

The method is not static so you need to actually instantiate the Form1 class.

Unknown_Coder
  • 764
  • 6
  • 24