Overclock.net banner

[C#] Form 2 Accessing Variables in Form 1

16539 Views 6 Replies 6 Participants Last post by  Knitelife
Hey Guys,

How do I access variables in Form 1 so I can use them on Form 2.

Thanks Guys,

Chris
1 - 7 of 7 Posts
Is Form2 created by Form1 or vice versa?

If Form1 creates Form2 then you can simply pass your variables to Form2 in the constructor or via Access functions (Get/Set).

If Form1 is created by Form2 you need to use Get functions to pull the values out.

I'll post example code when I get home from work if you need it.
See less See more
2
Quote:

Originally Posted by Safetydan View Post
Is Form2 created by Form1 or vice versa?

If Form1 creates Form2 then you can simply pass your variables to Form2 in the constructor or via Access functions (Get/Set).

If Form1 is created by Form2 you need to use Get functions to pull the values out.

I'll post example code when I get home from work if you need it.

That would be great! thanks!
See less See more
Code:

Code:
void Form1::Foo()
{
    // Pass just the data to Form2
    Form2 form2 = new Form2(A(), B(), C());

    // ...
}
or

Code:

Code:
void Form1::Foo()
{
    // Reduce coupling between the two objects by just passing a small/specific 
    // interface to Form2  i.e. don't pass the entire Form.
    Form2 form2 = new Form2((ISmallSpecificInterface)this);

    // ...
}

public interface ISmallSpecificInterface
{
    string A { get;  set; }
}
See less See more
Or you can create a public property in form1 so call the property from from2.

//In form1

private String _var1;

public String Var1
{
get { return _var1; }
}

//in form2.

private String _var1from1;
_var1from1=form1.Var1();
See less See more
  • Rep+
Reactions: 2
Quote:

Originally Posted by linskingdom View Post
Or you can create a public property in form1 so call the property from from2.

//In form1

private String _var1;

public String Var1
{
get { return _var1; }
}

//in form2.

private String _var1from1;
_var1from1=form1.Var1();

...You just taught me something today. Thank you.
See less See more
I really should watch this section of the forums more. There are a lot of knowledgable people in hear just tossing out tips and tricks I could use.

+rep linskingdom
1 - 7 of 7 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top