Regarding the scope when working with forms in .NET C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having the scope problem,I have two forms form1 and form2.
Form1 has two textboxes and they is a procedure called
public void callme()
{
text1.Text="Check"
}

In form2 when I try to reference this callme ,it say it need to be static procedure.
but when it's static .I get a error text1.text is nonstatic member reference.
How can I call a procedure from form2 to form1 that has reference to the controls .
Can some please help me in this regards,
Thanks
GP
 
GP said:
I am having the scope problem,I have two forms form1 and form2.
Form1 has two textboxes and they is a procedure called
public void callme()
{
text1.Text="Check"
}

In form2 when I try to reference this callme ,it say it need to be static procedure.
but when it's static .I get a error text1.text is nonstatic member reference.
How can I call a procedure from form2 to form1 that has reference to the controls .
Can some please help me in this regards,

You may have some confusion between classes and objects (class instances).

form1 and form2 are classes. There may be multiple instances of these
classes. Your "callme" method is an instance method of the class form1. That
means that it belongs to each individual instance of form1. That's the
opposite of being a static method. An instance method must always be
referenced through an instance:

form1 f1 = new form1();
f1.callme(); // But form1.callme() would be wrong.

If you had declared callme as "public static void callme" then it would be a
static method, that is, it would be a method which belongs to the entire
form1 class, not to any one instance of it. You'd be able to call
"form1.callme()".

The problem with that is that "text1" is an instance member of form1. There
is one text1 for each instance of form1. This is fine if text1 is referenced
from an instance method - the method belongs to a single instance and refers
to the text1 which belongs to that same instance. But if you try to
reference text1 from a static method, it won't compile, since the static
method belongs to the class as a whole, not to any one instance of the
class.

Perhaps you have a situation where there's only one instance of form1 and
form2? In that case, you can use the singleton pattern on the two forms.
Something like this:

public class Form1 : Form
{
private Form1() {} // So only I can construct it

private static Form1 _instance = new Form1(); // The only instance

public static Form1 Instance // The only
way to get Form1.Instance
{
get {return _instance;}
}

private TextBox text1;

public string callme()
{
return "Check";
}
}

public class Form2 : Form
{
private Form2() {} // So only I can construct it

private static Form2 _instance = new Form2(); // The only instance

public static Form2 Instance // The only
way to get Form2.Instance
{
get {return _instance;}
}

private TextBox text1;

public void callme()
{
text1.Text = Form1.Instance.callme();
}
}

HTH,
John Saunders
johnwsaundersiii at hotmail
 
Back
Top