CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    18

    Question How to access a form variable in a class?

    I have a form containing a textbox.(Form1).I want to acess and use the value of the textbox in form1 in my program(Program.cs).How do I do so?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to access a form variable in a class?

    Normaly you would pass it to a method in the class as a parameter.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to access a form variable in a class?

    1) If you intend to change the value directly, you generally shouldn't do that, Form1 should be responsible for this kind of stuff;
    2) however, it can be done - Form1 is a class like any other, and you would use the same approach as for any class - for example, you can expose the data via a property (and it's probably not a good idea to expose the texbox itself, but only the text).
    3) This is one of the most frequently asked questions on the forums, a search should come up with answers.

    You can find more info on properties here.
    Make sure you understand what a property is and what a backing field is.

    In your case, a backing field would be your textbox (check it's name in the designer - although its declaration doesn't show up in Form1.cs, it is declared in Form1.Designer.cs - these two files together define the Form1 class.)
    Now, you don't want to expose the text box, but only its Text property - so the type of your property should be string.

    Another option is to use a method, but properties are better suited for what you need to achieve.

    If you only want to read the value and use it outside the Form1 class, then you only need the get block - it's a good practice to prevent any unwanted behavior. This way, you won't be able to accidentally change the textbox text from outside the context of Form1.

    If you want to be able to change the value, you can either do it by providing a get block, or you can rely on events.

    In essence, the other class only needs a way to reach the stuff it needs. It can get it in various ways: via a property, as explained, via a method, or during construction, where it can be passed as a parameter to the constructor.

    A more advanced solution, which facilitates unit testing, separation of concerns, and reusability, involves using the MVP pattern, where you would have a separate data model (Model), a separate gui class (View), and a separate data presentation logic (Presenter) to be used with a gui.

    I'm sure you can find more info and links on each of these somewhere on the forums.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured