473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
5 11807
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
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
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
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> ¼¶¼g©ó¶l¥ó·s»D: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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
1
by: JM | last post by:
Hello, Using Access 2000 queries, you can reference(pass) form values directly using syntax like Forms!frmPaint!txtColor. I want to do a pass through query to SQL Server 2000, but I don't know...
7
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
8
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
14
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
6
by: =?Utf-8?B?Unlhbg==?= | last post by:
I am trying to pass a value from a texbox in Form1 to a textbox in Form2 using properties in VS2005 but it doesn't work; please help (project is attached). Code for Game Class: using System;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.