• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 913
  • Last Modified:

Get the Value from one form to another and Vice-versa C#

Hello Experts,

In my Windows C# application, I have form1 and form 2.

And written a class for accessing Values(get/Set) within two forms.When I Click button on form1, which opens form2 as a Popup where I have a textbox with inputs User Information.On button Close, I do just this.close() and back to form1.

When I open again form2 on button click, it should show me the last entered value,which I'm accessing via get/set methods written in Access Class.

Whenever, I try a new instance of Access Class, I get the values as null.Below is the code of Access class:
public class Access
    {
        private string _userData ;
        public string UserData
        {
            get { return _userData ; }
            set { _userData = value; }
        }
    
    }

Open in new window


In form2.cs I access like this:
Access UsrData = new Access() ;
tbUser.Text = UsrData .UserData;//Null Value returned

Open in new window

When I do this, I get the UserData value as Null, since I created a new Instance..How can I access value of UserData backnForth once the value is set.

Thanks,
ASPDEV
0
ASPDEV
Asked:
ASPDEV
  • 3
  • 2
1 Solution
 
ransommuleCommented:
You could use an static property.

private static string _userData ;
        public static string UserData
        {
            get { return _userData ; }
            set { _userData = value; }
        }
0
 
ASPDEVAuthor Commented:
When I did static, it doesn't show my UserData as a proeprty
0
 
ASPDEVAuthor Commented:
I tried this:

 private Access _usrData;
        //public Access UsrData
        //{
        //    get { return _usrData; }
        //    set { _usrData = value; }
        //}

Open in new window


And tried accessing UsrData.Comments, I get exception:"Object instance not created for this object", I mean UsrData is Null.
0
 
ransommuleCommented:
this is because the call must be something like

tbUser.Text = Access.UserData;

Access is the name of the class not the name of the instance
0
 
ASPDEVAuthor Commented:
Worked.

Thanks,
ASPDEV
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now