Solved

Accessing public variable

Posted on 2012-03-25
12
385 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
  • 7
  • 5
12 Comments
 
LVL 74

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 74

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now