|
-
Sep 28th, 2007, 08:53 AM
#1
Thread Starter
Hyperactive Member
public variables calling in different forms C#
I have two forms
form1 and form2
form1 is the main form.
i have created variable in form1 named "getName" and set the value with "Computers"
now on loading form2 on click of button in the form1, on the load event i want show the value of string : getName in the form1 ....
-
Sep 28th, 2007, 09:04 AM
#2
Re: public variables calling in different forms C#
-
Oct 2nd, 2007, 09:21 AM
#3
Lively Member
Re: public variables calling in different forms C#
If you simply want to display a string from one form onto another. i would pass it through on the constructor. Probably not the best way but it will get the job done.
Form 1 Code
Code:
private void form1Button_Click(object sender, EventArgs e)
{
string passString = "this is our string";
Form2 myForm = new Form2(passString);
myForm.ShowDialog(this);
}
Form 2 Code using the constructor.
Code:
public Form(string stringFromForm1)
{
_form1String = stringFromForm1;
InitializeComponent();
}
private string _form1String;
public string form1String
{
get { return _form1String; }
set { _form1String = value; }
}
I used a property on form 2 even though you wouldn't have to.
hope this helps.
-zd
-
Oct 29th, 2007, 12:12 PM
#4
Junior Member
Re: public variables calling in different forms C#
Hey,
How would you do this if you are trying to pass an array to another form??
Thanks,
Dylan
-
Oct 29th, 2007, 12:38 PM
#5
Addicted Member
Re: public variables calling in different forms C#
get() and set() methods are the best ways to access fields/attributes of other classes/forms. You should never make fields public, since it can/will make your classes depend on how this class is implemented, also called high coupling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|