Hi
Ijust started learning c# windows forms and when i set soe data open a new form and get the data into the label the label dissappears.can some one help me please

private string greeting;

        public string Greeting
        {
            get
            {
                return greeting;
            }
            set
            {
                greeting = value;
            }
        }

        
        
        
        public Class1()
        {       
        }
Class1 a = new Class1();

        public Form1()
        {
            InitializeComponent();
      

        }

        private void label1_Click(object sender, EventArgs e)
        {
            label1.Text =("hello");

            a.Greeting = "hello";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 F2 = new Form2();
            F2.Show();
           
        }
public partial class Form2 : Form
    {
        Class1 b = new Class1();
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = b.Greeting;
        }

You have created two instances - each in separate form.

Class1 a = new Class1();
        public Form1()
        {
            InitializeComponent();
        }
        private void label1_Click(object sender, EventArgs e)
        {
            label1.Text =("hello");

            a.Greeting = "hello";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 F2 = new Form2();
            F2.PassRef(a);
            F2.Show();
        }
public partial class Form2 : Form
    {
        Class1 b;
        public Form2()
        {
            InitializeComponent();
        }
       public void PassRef(Class1 obj) {
            b=obj;
        }
        private void button1_Click(object sender, EventArgs e)
        {
         if(b!=null) 
            label1.Text = b.Greeting;
        }

i get an error

Error 1 Inconsistent accessibility: parameter type 'WindowsFormsApplication3.Class1' is less accessible than method 'WindowsFormsApplication3.Form2.PassRef

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.