Accessing public variable

HI,

I am trying access value from 'Form2' into 'dbo2' class but ended up with error. how can I access txtRef in the dbo2 class?

the error

Error      1      The name 'txtRef' does not exist in the current context      

namespace myProject
{
   public partial class Form2: Form
   {
       public Form2 ( )
       {
          InitializeComponent ();
       }

       private void Form2_Load (object sender, EventArgs e)
       {

       }
       }

       private string txtRef=string.Empty;

       public string _Ref
       {
          set { txtRef=value; }
       }
   }

   public class dob2: DataSet
   {
       private DataTable dt2;
       string str = txtRef;   <--- values to be accepted here.

       public dob2 ( )
       {
      }
}

}

Thanks

ayha
LVL 7
ayha1999Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You have made the _Ref property write-only since you only provided a set, and the txtRef field is private, so no outside code is going to have access to that field. The simplest solution would be to add a get to your _Ref property and then you can access the data from outside:

public string _Ref
{
    get { return txtRef; }
    set { txtRef=value; }
}

Open in new window

ayha1999Author Commented:
still same error. may be something wrong in my implementation.

Error      1      The name 'txtRef' does not exist in the current context

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

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private string txtRef = string.Empty;

        public string _Ref
        {
            get { return txtRef; }
            set { txtRef = value; }
        }
    }

    public class DBO2 : DataSet
    {
        string str = txtRef;

        public DBO2()
        {

        }
    }
käµfm³d 👽Commented:
You don't have any way of accepting data in your DBO2 class. Can you show an example of how these two classes interact?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

ayha1999Author Commented:
Actually what I trying is to get a value Form1 in Form2 or DBO2 class.  the follwoing method as I mentioend in the question works in Form2. I can access value from Form1 into Form2.
 private string txtRef=string.Empty;

       public string _Ref
       {
          set { txtRef=value; }
       }

but I want to access the value in DBO2 too. If can access value from Form1 directly into DBO2, then no need the value be available in Form2.
käµfm³d 👽Commented:
the follwoing method as I mentioend in the question works in Form2
It shouldn't. As I mentioned, the code you posted does not provided any way to retrieve data. The property is write-only (only has a set), and the backing variable is private. This data is not accessible to anything outside of this class.

Perhaps this might help:  http://www.experts-exchange.com/Programming/Languages/.NET/A_4322-How-to-Pass-Data-Between-Forms-in-NET.html

The article mentions Forms specifically, but since all Forms themselves are classes, the concepts would still apply.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ayha1999Author Commented:
Form2:
       public string _Ref
       {
          get { return txtRef.Text; }
       }
 
Button click event in form2:

 Form1 frm=new Form1 ();
frm._Ref=_Ref;
 frm.Show ();
--------------------------------------------------

Form1:

private string txtRef=string.Empty;
 public string _Ref
       {
          set { txtRef=value; }
       }

and it the value of textbox control in form2 is available in form1.

I will check the article provided.

Thanks
käµfm³d 👽Commented:
The difference above is that Form2 has provided a get. This data *is* accessible to outside classes.

What's going on above is that you are within Form2. While there, you create a new instance of Form1. Since you have made Form1's _Ref property write-only, and you have made Form2's _Ref readable, the assignment of Form2's _Ref to Form1's _Ref is valid.

Where are you creating a new instance of dob2?
ayha1999Author Commented:
in Form2
ayha1999Author Commented:
Ok. may like this

        private void Form1_Load(object sender, EventArgs e)
        {
            DBO2 db2 = new DBO2();
            db2.myVal=txtRef;
        }

        public string _Ref
        {
            //get { return txtRef; }
            set { txtRef = value; }
        }
        public string txtRef = string.Empty;

    }

    public class DBO2 : DataSet
    {
        public string myVal;
käµfm³d 👽Commented:
Yes, that would make a bit more sense (though you generally don't expose fields [variables] to outside classes--i.e. create a property which exposes them).
ayha1999Author Commented:
Thanks
ayha1999Author Commented:
Hi kaufmed,

The solution was expcted to work but I got some problem in accessing in the class. could you please have look at the following question?

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27648170.html

thanks

ayha
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.