CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

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

    Re: c# access variable from other namespaces

    bluRemote is the name of the class - a type that is. You need to create an instance of that type (an object) in order to access it's members.

    To avoid writing fully qualified names (bluRemote instead of BluMote.bluRemote), add
    using BluMote;
    to the top of the file where you want to use the class.


    Then, to create a new instance, write:
    bluRemote theRemote = new bluRemote();
    (Or the other class can get it as a parameter.)


    Then you'll be able to access it's members.
    if(theRemote.cableOrSat == "CABLE")
    //...

    BTW:
    It's not a good practice to publicly expose member fields of a class - use properties instead (basically, a syntactic sugar for get/set methods).
    See: Properties (C# Programming Guide)
    For simple getters/setters, C# provides a feature called auto-property, where the backing field is implicit:
    Code:
    public string CableOrSat { get; set; }
    //or
    public string CableOrSat { get; private set; } //can be set in the constructor
    Finally, it's better to chose a more generic, yet descriptive name, like Cathegory instead of CableOrSat, so that you can potential support support more than just "CABLE" and "SAT" in the future - a general recommendation, not necessarily pertaining to this specific app.

    Also, the coding convention for C# states that types (like classes), methods, and properties should be written in PascalCasing (note the first capital letter), and variables and parameters in camelCasing. That way, other C# devs will be able to figure out your code more quickly (should you post a longer snippet) and are more likely to help you out.
    Last edited by TheGreatCthulhu; August 8th, 2011 at 04:51 PM.

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