0

I have a main form and I need in other class get or set field from Main Form. I created public property in Main Form:

public partial class MainForm : Form
{
     public string get_txt()
     {
         return phone.Text;    //phone - textbox
     }
}

But in other class I can't get this property:

MessageBox.Show(MainForm.get_txt); //there error. It doesn't seemed get_txt property.

I'm sorry for this simpliest question, but I really don't know this. Everywhere wrote that simpliest way to do it - in class of Main Form create public property for needed private field in Main Form. What do I wrong?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Dima
  • 9
  • 2
  • 2
    `MainForm` is the class itself, not an instance of that class. – Equalsk Aug 03 '17 at 10:52
  • 2
    Possible duplicate of [Accessing variables in other Windows Form class](https://stackoverflow.com/questions/12042303/accessing-variables-in-other-windows-form-class). Also: [How to: Get a Value from Another Form](https://stackoverflow.com/questions/12042303/accessing-variables-in-other-windows-form-class). – Ian H. Aug 03 '17 at 10:53
  • What @Equalsk said plus you have the value exposed through a method not a property. If you keep it the way it is you'll need to use `formInstance.get_txt();` – Stephen Wilson Aug 03 '17 at 10:54
  • But if it initialize in begining of program, instance is creating, yes? Application.Run(new MainForm()); How I can get instance of main form? – Dima Aug 03 '17 at 10:55

4 Answers4

0

So the MessageBox.Show(MainForm.get_txt) won't compile as you're trying to refer to a static property, not an instance method.

I'm not a winforms dev, so this is a bit of a guess:

Somewhere in your code you will have a call like var main = new MainForm(). On that instance i.e. main you will be able to call main.get_txt().

As a side note, given that you're working in C# it'd be good to name the method to something a little more idiomatic, perhaps public string GetPhoneNumber() or better yet make it a property:

public string PhoneNumber { get { return phone.Text; } }

ilivewithian
  • 19,476
  • 19
  • 103
  • 165
  • this will not automatically ensure that the string written in `phone.Text` will be displayed. Since a new `MainForm` is created. – Mong Zhu Aug 03 '17 at 11:05
  • Hence the reference to 'somewhere in your code' i.e. use the instance you already have. – ilivewithian Aug 03 '17 at 11:57
0

Make instance of MainForm and than call it.

Mainform _mainform=new Mainform();

MessageBox.Show(_mainform.Get_text);
Farruk
  • 31
  • 1
  • 9
  • this will not automatically ensure that the string written in `phone.Text` will be displayed. Since a new `MainForm` is created. – Mong Zhu Aug 03 '17 at 11:05
0

Your MainForm is not static class, you can not use like that. If u want access the MainForm(methods or variables), you have to assing the variable.

MainForm mForm = new MainForm();
MessageBox.Show(mForm.getText());
OnePage
  • 116
  • 1
  • 13
  • 1
    this will not automatically ensure that the string written in `phone.Text` will be displayed. Since a new `MainForm` is created. – Mong Zhu Aug 03 '17 at 11:05
  • "Your MainForm is not static class" this is not necessary, the class doesn't have to be static. Only the property to be accessed this way. But this also won't work, because it is accessing a class variable `phone` – Mong Zhu Aug 03 '17 at 11:13
  • @MongZhu `MainForm.get_txt` if u want use like that, your class have to be static. – OnePage Aug 03 '17 at 12:53
  • no the class does not have to be static. The method or property has to be static. – Mong Zhu Aug 03 '17 at 13:00
0

Thanks to you all. but when I create a new istance of Main Form all controls in it will be initializing like in my current Main Form?

Dima
  • 9
  • 2
  • no they won't (unless you hardcoded the values in the controls), as I wrote in my comments below the other answers. Please use the comment button to leave a comment and don't post answers that are comments – Mong Zhu Aug 03 '17 at 11:10