?
Solved

Accessing public variable

Posted on 2012-03-25
12
Medium Priority
?
396 Views
Last Modified: 2012-03-26
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
0
Comment
Question by:ayha1999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37762906
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

0
 
LVL 7

Author Comment

by:ayha1999
ID: 37762927
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()
        {

        }
    }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37762942
You don't have any way of accepting data in your DBO2 class. Can you show an example of how these two classes interact?
0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 7

Author Comment

by:ayha1999
ID: 37762959
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.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 37762979
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.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 37762997
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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37763010
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?
0
 
LVL 7

Author Comment

by:ayha1999
ID: 37763016
in Form2
0
 
LVL 7

Author Comment

by:ayha1999
ID: 37763030
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;
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37763037
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).
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 37764380
Thanks
0
 
LVL 7

Author Comment

by:ayha1999
ID: 37764810
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
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question