Solved

Accessing public variable

Posted on 2012-03-25
12
392 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Get Current Exchange Rate from XE 9 42
Pdf not loading 2 39
Issue in displaying table columns on selection of dropdown 2 27
dropdownlist in asp.net vb. 3 21
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

734 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