Search code examples
c#methodsfieldencapsulation

How to use a field in another form file?


public partial class Form3 : Form
{
        public Form3()
        {
            InitializeComponent();   
        }

        int port;       // I declared a variable and I wanna use this in another form like
}

// ------------------------------------------------------- //

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
            SagTikMenuOlustur();
        }

        void menu1_Click(object sender, EventArgs e)
        {
            Form2 frq = new Form2();
            frq.Show();

            MessageBox.Show("{0} server is online ",port); //How to I declare ????
        }

}

Solution

  • Best thing would be to create a property for it.

    Try this

    public partial class Form3 : Form
    {
        int _port;
        public int Port
        {
           get { return _port; }
           set { _port = value; }
        }
    }
    
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }
    
        void menu1_Click(object sender, EventArgs e)
        {
            Form2 frq = new Form2();
            frq.Show();
            Form3 frm3 = new Form3();
            frm3.Port = 8080;
    
            MessageBox.Show("{0} server is online ", frm3.Port);
        }
    
    }