Visual C# 2003 how to send variables in a class to other forms?

Hello

1) I have 2 forms and in the first form "form1" I have som textboxes and then I will put the data fra form1 textboxes into some variables in a class and then take the whole class and variables in the class to form2, so I can take out the data from form1 and use in form2

2) In another programering langauge you have to delete a class instans, but do I do that in Visual C# 2003, and is it neccesary or not

Hope to hear from someone.

Thanks

spot



Answer this question

Visual C# 2003 how to send variables in a class to other forms?

  • VC_Mike

    Can you give some samples in syntax
  • Greg E

    You must format your number before assigning it to the textbox. Read about basic formatting in this "How To" tutorial:

    http://www.dotnetjunkies.com/quickstart/howto/doc/format.aspx


  • DaveV

    In Form2 you can create a custom property of type of your class and set that property before you show your Form2.

    If you create the form instance you don't have to explicitly set to null (or Nothing in VB), Garbage collector takes care of it when the instance variable goes out of scrope. You can always call the Dispose() method though to release resources used by the form.


  • Dans.

    Thanks

    1) But how do I use properties as you said you do in real world

    2) Another thing how do I do that the text in textboxes fill in the variables in the class then you can get them but press af button in form2 and also the automatic put in some textboxes in form2 by using the class

    Can you/other help me how it will be in syntax.

    Thanks

    spot


  • Aravinda

    Ok, let's say on Form1 you have employee's First Name and Last Name and a button to launch Form2. You also have Employee class which you will be using to pass data between the two forms. As I said earlier, you can have a custom property on Form2, set it before launching it.

    Here's how Employee class looks like:

    public class Employee

    {

    //I'm using fields for demonstration only.

    //In real world you should be using properties

    public string FirstName;

    public string LastName;

    }

    On Form2 add a button that displays information from custom Employee property:

    private Employee _employee = null;

    public Employee Employee

    {

    get { return _employee; }

    set { _employee = value; }

    }

    private void button1_Click(object sender, System.EventArgs e)

    {

    if (_employee != null)

    MessageBox.Show(_employee.FirstName + " " + _employee.LastName);

    else

    MessageBox.Show("Employee property not set.");

    }

    Finally in Form1 you create a Employee object and pass it to Form2:

    private void launchForm2Button_Click(object sender, System.EventArgs e)

    {

    Employee emp = new Employee();

    emp.FirstName = firstNameTextBox.Text;

    emp.LastName = lastNameTextBox.Text;

    Form2 form = new Form2();

    form.Employee = emp;

    form.Show();

    }

    Hope this helps.


  • MarkD84

    Considering the same example, when you promote public fields as properties, Employee class would look like:

    public class Employee

    {

          private string _firstName;

          private string _lastName;

     

          public string FirstName

          {

                get { return _firstName; }

                set { _firstName = value; }

          }

     

          public string LastName

          {

                get { return _lastName; }

                set { _lastName = value; }

          }

    }

    No changes to Form1 code for launching Form2 or using the Employee class. That's the beauty of it. Now let's say in Form2 you have a TextBox and you have a text box where you want to concatenate first and last name and show it when the Form2 is opened. For that you would write the same code in Form2's Load event instead of Button's Click event:

    private void Form2_Load(object sender, System.EventArgs e)

    {

          if (_employee != null)

                employeeNameTextBox.Text = _employee.FirstName + " "  + _employee.LastName;

    }

     


  • BillDude

    I have a problem when I should send a double from textbox in form1 and then it will be show in form2 you se it without "dot"(Not as double data), and I have try both "parse" and "convert" but it don't work...


  • Visual C# 2003 how to send variables in a class to other forms?