Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my wpf application, I have window 1, window 2, and a separate class file called share.cs with a public integer variable 'a'

I have a textbox and a button in each of the windows.

on the button in window1, i perform the following operation

Window1.cs
C#
private void button1_Click(object sender, RoutedEventArgs e)
       {
           share s = new share();

          s.a =  Convert.ToInt32(textBox1.Text);
          Window2 win = new Window2();
          win.Show();

       }

Basically, i just want to copy the value from textbox on window1 into the variable in share class, and then display the window2.

Now, In window2, I would like to retrieve the value from variable 'a' in share class and would like to display it on the textbox in window2, when i click on the button in window2. AND it is my understanding that the instance 's' in window1 is different from that of window2, and that is the reason why i am not getting the output correctly.

Window2.cs
C#
private void button1_Click(object sender, RoutedEventArgs e)
        {
            share s = new share(); 
            textBox1.Text = Convert.ToString(s.a);
        }



Is there anyway, that I could create a single common instance and use it in both the classes ? If so, where should I create the instance and make it available to both the windows ? i just want both the windows to use the variable 'a' in share class using same instance.
This is what I have in share.cs.

C#
public class share
    {
       public int a;
    }


I am a learner and I need direct answers please; not suggestions. Any help would be truly appreciated. Thanks. :)
Posted
Updated 8-Sep-11 12:47pm
v2
Comments
Philippe Mori 8-Sep-11 19:11pm    
The easy answer would be to add static modifier to a. But anyway, it is generally a bad idea to share data through global data that will eventually lead to unmaintanable code as the application will grow.

1 solution

The easiest way that could be considered a good design would uses properties of the Form.

There are other possibilities related to data binding and depedency properties but I haven't played much with WPF yet so I still do some things the WinForms way...

1) Add a property in Window2

C#
// Window2.cs
public int A { get; set; }


2) And the set that property in the click handler:

C#
// Window.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
    Window2 win = new Window2();
    win.A = Convert.ToInt32(textBox1.Text);
    win.Show();
}


3) And the get the data when the button in the second form is clicked:
C#
// Window2.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
    textBox1.Text = Convert.ToString(A);
}
 
Share this answer
 
Comments
steersteer 8-Sep-11 20:40pm    
Thank you very much. I am getting the correct output now. Can i follow the same design procedure for arrays or lists as well ? I have two textboxes in window1 now and I would like to store their values in A[0] and A[1] respectively in window2.
Philippe Mori 8-Sep-11 20:58pm    
For maintainabily purpose except if the data represent an array, you should use distinct properties with meaningfull names.
_Maxxx_ 8-Sep-11 21:30pm    
If you want to pas multiple data items (be they integers, strings, arrays etc.) I would suggest having those items as properties of your 'Share' class. Instantiate the class in window1, and have a public property of type share in Window2.
That way you nicely encapsulate the data being shared into a single object, and avoid having many properties on which the communication between the windows depend.

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