locked
Passing values between two classes RRS feed

  • Question

  • Hi I am currently working on a project that requires me to pass a DateTime value from MainForm to Appointments.

    I have a combo box which I want to pass the date from that to another form to put into a textbox.
    Wednesday, June 11, 2014 1:38 PM

Answers

All replies

  • There are two requirements for one object (A) to set the value of another one (B):

    1. The value or a proper setter function in B must be public.

    2. The instance of A needs a reference to teh isntance of B

    So the big question is: Where are appointements stored? Are those classes created by the main form? Is there some form of application wide storage that all Forms share*? Are both forms, if so when and how is Appointments created?

    *WinForms lacks proper support for application wide variables that all forms share, but you can get something close by using static variables.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


    Wednesday, June 11, 2014 2:11 PM
  • Please refer to my answer here:

    http://social.msdn.microsoft.com/Forums/vstudio/en-us/03a738df-d59d-47c4-a211-f4dd97b97106/pasing-data-between-forms?forum=csharpgeneral

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Thursday, June 12, 2014 10:03 AM
    • Marked as answer by Anne Jing Sunday, June 22, 2014 8:56 AM
    Wednesday, June 11, 2014 2:36 PM

  • Abu Ehab Developer

    Wednesday, June 11, 2014 10:53 PM
  •  // In Class1 :
    public  class Class1
        {
           public static string TheName = "";
        }
    }
    
    // In Form1 :
    
      private void button1_Click(object sender, EventArgs e)
            {
                Class1.TheName = this.textBox1.Text;
                Form Frm = new Form2();
                Frm.Show();
            }
    
    // In Form2 :
     private void Form2_Load(object sender, EventArgs e)
            {
                this.textBox1.Text = Class1.TheName;
            }


    Abu Ehab Developer

    Wednesday, June 11, 2014 10:57 PM