By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,952 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,952 IT Pros & Developers. It's quick & easy.

In C#, pass values between 2 forms and one Global.cs

P: n/a
Hi,

In Global.cs, I have public string myString="".
Another 2 forms are Form1.cs, Form2.cs.
How do I use the Global.cs' myString to store a string in Form1 and then
pass to Form2?
Thanks for help.

Jason
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Jason,
I would add a property to Form2 which allows you to pass in a string i.e.

class Form2 : Form
{
private string _thePassedInString;

public string NewFormString
{
set
{
_thePassedInString = value;
}
}

}
so now you would be able to say something like
Form2 f = new Form2();
f.NewFormString = "abcd";

If you want to store data from one place i.e. Form1 and pass it to Form2 at
a later point in time when Form1 is no longer available then I would create a
DataStore object, which is a singleton so you only ever have one instance,
like:

class GlobalDataStore
{
private static GlobalDataStore _instance = new GlobalDataStore();
private string _formString = "";

static GlobalDataStore()
{
}

public static GlobalDataStore GetInstance()
{
return _instance;
}

public string FormString
{
get
{
return _formString;
}
set
{
_formString = value;
}
}
}

Now you can use this object to store and retrieve your data from anywhere i.e.

//Inside Form1
GlobalDataStore.GetInstance().FormString = "abcd";
//Inside Form2
string myStringValue = GlobalDataStore.Getnstance().FormString;
Hope that helps
Mark R Dawson
"Jason Huang" wrote:
Hi,

In Global.cs, I have public string myString="".
Another 2 forms are Form1.cs, Form2.cs.
How do I use the Global.cs' myString to store a string in Form1 and then
pass to Form2?
Thanks for help.

Jason

Nov 17 '05 #2

P: n/a
Jason,

I'm assuming that Form1 and Form2 are separate classes and can't "see" each
other??? If you try to connect the two, you get a cyclical error?

If so, I have two things to think about:

1. If the forms provide similar tasks, you could create a base class to hold
the "global" or common items and then inherit each form from the baseform.
This would make the myString field automatically visible to both forms and
all others derived from baseform. (As long as myString is public or
protected.)

2. If it doesn't make sense to have a base class, you could use a delegate
in Global that fires off a method in Form2.cs that sets contents of
Global.myString to Form2.myString. The delegate method would be assigned by
Form2 when Form2 is created. Form1 would set Global.myString, then Form1
would call/invoke the delegate, which assignes Global.myString to
Form2.myString (because the method running is really
Form2.myStringFetchMethod.) Sounds a little complicated but it is really
pretty easy to do.

I prefer the base class technique when possible but delegation works great
too.

If you need some code examples, let me know and I'll work one up for you.

Charlie
:-)

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Hi,

In Global.cs, I have public string myString="".
Another 2 forms are Form1.cs, Form2.cs.
How do I use the Global.cs' myString to store a string in Form1 and then
pass to Form2?
Thanks for help.

Jason

Nov 17 '05 #3

P: n/a
Hi ,
make variables static

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Hi,

In Global.cs, I have public string myString="".
Another 2 forms are Form1.cs, Form2.cs.
How do I use the Global.cs' myString to store a string in Form1 and then
pass to Form2?
Thanks for help.

Jason

Nov 17 '05 #4

P: n/a
Thanks Charlie,

I think the base class method seems easier to me.
But Form1 has "public class Form1 : System.Windows.Forms.Form" and Form2 has
"public class Form2 : System.Windows.Forms.Form" already, how can I have the
Form1 and Form2 to inherit from Global? What's the syntax?
Jason
"Charlieee" <ch*******@newsgroup.nospam> glsD:ei**************@TK2MSFTNGP09.phx.g bl...
Jason,

I'm assuming that Form1 and Form2 are separate classes and can't "see"
each other??? If you try to connect the two, you get a cyclical error?

If so, I have two things to think about:

1. If the forms provide similar tasks, you could create a base class to
hold the "global" or common items and then inherit each form from the
baseform. This would make the myString field automatically visible to both
forms and all others derived from baseform. (As long as myString is public
or protected.)

2. If it doesn't make sense to have a base class, you could use a delegate
in Global that fires off a method in Form2.cs that sets contents of
Global.myString to Form2.myString. The delegate method would be assigned
by Form2 when Form2 is created. Form1 would set Global.myString, then
Form1 would call/invoke the delegate, which assignes Global.myString to
Form2.myString (because the method running is really
Form2.myStringFetchMethod.) Sounds a little complicated but it is really
pretty easy to do.

I prefer the base class technique when possible but delegation works great
too.

If you need some code examples, let me know and I'll work one up for you.

Charlie
:-)

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Hi,

In Global.cs, I have public string myString="".
Another 2 forms are Form1.cs, Form2.cs.
How do I use the Global.cs' myString to store a string in Form1 and then
pass to Form2?
Thanks for help.

Jason


Nov 17 '05 #5

P: n/a

please send me an example with code and try to use mdi application.
thank you.
Charlieee wrote:
Jason,

I'm assuming that Form1 and Form2 are separate classes and can't "see" each
other??? If you try to connect the two, you get a cyclical error?

If so, I have two things to think about:

1. If the forms provide similar tasks, you could create a base class to hold
the "global" or common items and then inherit each form from the baseform.
This would make the myString field automatically visible to both forms and
all others derived from baseform. (As long as myString is public or
protected.)

2. If it doesn't make sense to have a base class, you could use a delegate
in Global that fires off a method in Form2.cs that sets contents of
Global.myString to Form2.myString. The delegate method would be assigned by
Form2 when Form2 is created. Form1 would set Global.myString, then Form1
would call/invoke the delegate, which assignes Global.myString to
Form2.myString (because the method running is really
Form2.myStringFetchMethod.) Sounds a little complicated but it is really
pretty easy to do.

I prefer the base class technique when possible but delegation works great
too.

If you need some code examples, let me know and I'll work one up for you.

Charlie
:-)

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
Hi,

In Global.cs, I have public string myString="".
Another 2 forms are Form1.cs, Form2.cs.
How do I use the Global.cs' myString to store a string in Form1 and then
pass to Form2?
Thanks for help.

Jason


Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.