Edward Keenan

I am not new to programming, but I am relatively new to C# and Object-oriented programming.

Here is my situation. I have a base class with T1, T2, T3, and T4 as type string. T1 is to be used in form1. The remaining variables will be utilized in form2. T2, T3, and T4 will be populated from individual files and sent back to form1 to be used.

So far I have been using the following at the top of both forms:

"Test1 testdata = new Test1()"

Using this method, I would access lets say T2 in form2 by using

"testdata.T2"

In form1 I use the following to access the method called GetOptions() in form2 which has a return statement

testdata = newform2.GetOptions()

This method works for me, but T1 which gets populated in form1 gets wiped out when I am finished with the process of using form2. I know the reason why, but at this point I am trying to come up with a more effective way of sharing the above variables between 2 forms and I am not getting anywhere at this point. I have a basic understanding of constructors. Is it possible that the above method I was initially trying is not the proper way

Edward K.



Re: Visual C# Express Edition Sharing of data between forms.

Edward Keenan

Update:

At this point, I have been able to utilize the 2 parameter constructor in form2 for t2 & t4. I still have the problem of having the contents of T1 erased if I load up T1 first than run the process for form2. I have to run the process for form2 first than load T1 via file stream. I tried utilizing the single constructor for T1, but for some reason it did not work out very well so I am using testdata.T1 to work with T1 under form 1. Evidently I am missing something here, but what

Edward K.





Re: Visual C# Express Edition Sharing of data between forms.

Nayan Paregi

Hi

If u want to share data between the diffrent forms. than u have to need to declared as static object in Program.cs File. or in anyother Static Class

create the object of Test1 in the Program clas and use that object across the diffrent forms. by calling it using Program.test1object...






Re: Visual C# Express Edition Sharing of data between forms.

Edward Keenan

Thanks for the advice. I wound up converting T1 to a static variable and this solved the problem. I thought of using static variables before, but I wanted to see if I would have been able to solve it using non-static variables.

Edward K.





Re: Visual C# Express Edition Sharing of data between forms.

rauhanlinnake

Hi.

You can share data without using static variables, since they are not always what you need. If you have many form instances of the same form class, you most definetly cannot use static variables to share their data.

You could create an interface class, which can be used to access the other forms data. Interfaces makes it easier to modify your application, for example add another kind of form which's data can be shared in the same way.

Here is a small example, which illustrates a data sharing between forms using interface class and references between forms.

1. Create a new Windows Application. You should now have Form1 added, this is your main form
2. Add 2 new forms, Form2 and Form3
3. Add new cs file, and add following interface to it:
public interface IMyInterface
{
string Message
{
get;
set;
}
}

This is used to exchange data between forms
4. Open Form3 in design mode, and add a textbox in it
5. Open Form3.cs, and add following code to it:
public partial class Form3 : Form, IMyInterface
{
public Form3()
{
InitializeComponent();
}

#region IMyInterface Members

public string Message
{
get
{
return textBox1.Text;
}
set
{
textBox1.Text = value;
}
}

#endregion
}
There you can see the usage of the interface. Other forms can get access to your forms provate data via this interface
6. Open Form2 in design mode, and add a textbox and two buttons to it
7. Open Form2.cs, and add a following code to it:
public partial class Form2 : Form
{
IMyInterface _otherForm = null;
public Form2(IMyInterface otherForm)
{
_otherForm = otherForm;
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
_otherForm.Message = textBox1.Text;
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = _otherForm.Message;
}
}
In the Form2 constructor, you can see that you pass a IMyInterface as a parameter. After saving this reference, you can access the other forms information. Note, that the event handlers must be registered by double clicking the buttons, naturally.
8. Add a button in Form1
9. Create a event handler to it by double clicking
10. Add following code to Form1.cs:
public partial class Form1 : Form
{
Form3 form3;
Form2 form2;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
form3 = new Form3();
form2 = new Form2(form3);

form3.Show();
form2.Show();
}

}

Compile and run. When you press the button on the shown form (Form1), 2 forms appear. When pushing the buttons on the other form, you can set and get the text from another form.

Hope this helps to build your own application, without using the static variables.

Another way would be, that you use your main form (or other class where the forms are shown from) as a intermediate, which handes the data routing. This can be done so that you pass a reference to the owning form, and implement the owner so, that it reflects the data changes to other forms as well.

Cheers, Antti





Re: Visual C# Express Edition Sharing of data between forms.

Edward Keenan

Antti,

Thanks for the information. I would have never thought that the property code can actually be used within the form itself. I usually only see it in the class file. I am only using two books at the moment:Murach's C# 2005 and the "How To Program Book" from Deitel. Are there any other books that you would recommend or maybe even an online site that does not hold back on information that you just provided Its possible my initial idea goes beyond what my current books cover. You mentioned another way in your post. I would like to request to see that other option as well, if you do not mind of course

Thanks,

Edward K.





Re: Visual C# Express Edition Sharing of data between forms.

raghu krishna k

Hi,

If you want to share data between two objects, try to follow the steps:

1. create a shared class and declare all shared variables in that.

2. create an object for that shared class in starting form means form1.

3. and while creating an object for form2 pass this shared object to form2 by reference.

4. then automatically form1 and form2 contains shared object reference.

hope this will work

all the best