tags:

views:

65

answers:

2

Hi,

I want to pass a C# object between win forms. At the moment, I have setup a basic project to learn how to do this which consists of two forms - form1 and form2 and a class called class1.cs which contains get and set methods to set a string variable with a value entered in form1. (Form 2 is supposed to get the value stored in the class1 object)

How can I get the string value from the object that was set in form1? Do I need to pass it as a parameter to form2?

Any comments/help will be appeciated!

Thanks,

EDIT: Here's the code I have at the moment: (form1.cs)

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();

        Form2 form2 = new Form2();

        form2.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
        {
            Class1 class1 = new Class1();

            class1.setStringValue(textBox1.Text);
        }
    }
}

}

+2  A: 

Yes, in Form1 you declare an instance of Class1 and then set the parameters as needed, then you pass it to Form2. You could for example have a constructor in Form2 and have a Class1 parameter in it. Assuming that Form1 creates Form2, otherwise you have to have some way for Form1 to find Form2 to pass the instance across.

ho1
Thanks for your reply. I have eddited my question and added the code for form1.cs - how can I pass the object instance to form2 since form2.show() is being called when a different button is clicked? Could you provide an example?
aspdotnetuser
Rather than declaring `Form2 form2 = new ...` in the button1 event handler, just declare it as a member of the class. That way you'll be able to call `Form2.Show()` in the button1 event handler and you can also call something like `From2.ShowNewValues(class1)` in the button2 handler.
ho1
Here you can find a sample of a class member: http://www.csharp-station.com/Tutorials/Lesson07.aspx
ho1
That makes sense, but would I have to have get and set methods in class1.cs and a get method in form2.cs to get data stored in the object instance of class1? Or would I call the get method in the form2 method ShowNewValues()?
aspdotnetuser
Either way, but I'd probably do it that in the ShowNewValues method you'd call class1.GetSomething to get out the values and then use them right away. Though you might want to store copy of the whole of class1 instance as a member of Form2 if you're going to use the same values later for example.
ho1
+2  A: 

There are a few different ways to do this, you could use a static class object, the above example would be ideal for this activity.

public static class MyStaticClass
{
  public static string MyStringMessage {get;set;}
}

You don't need to instance the class, just call it

MyStaticClass.MyStringMessage = "Hello World";
Console.WriteLine (MyStaticClass.MyStringMessage);

If you want an instance of the object you can pass the class object that you create on Form1 into Form2 with the following.

private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form2 form2 = new Form2();
        form2.MyClass = class1;
        form2.Show();
    }

Then create a property on Form2 to accept the class object.

public Class1 MyClass {get;set;}

remember to make the Class1 object a global variable rather than create it within button 2 itself.

Sres
Thanks for the code snippet, Sres. Would you use a static class, for example, if you wanted to have a class used to store things that don't require many instances, like settings for an application? Whereas, a standard class could be used to create multiple instances if necessary, like user objects?
aspdotnetuser
Exactly that, I use static classes in a number of places, to hold database Connection details, CurrentUser details.Just be careful, especially if you get data that could change, you may end up with out of date information.
Sres