Solved

Accessing public variable

Posted on 2012-03-25
12
393 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 250 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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