CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2008
    Posts
    114

    Exclamation c# access variable from other namespaces

    I am trying to set/read a variable in class bluRemote from another namespace/class like so:

    Code:
    namespace BluMote
    {
        class bluRemote
        {
            public string cableOrSat = "CABLE";
           ........
        }
    Code:
    namespace BluMote
    {
        public partial class SettingsForm : Form
        {
            if (BluMote.bluRemote.cableOrSat == "CABLE")
            {
                 BluMote.bluRemote.cableOrSat = "SAT";
            }
         .......
        }
    }
    I know i am doing it wrong but im more used to doing stuff like this in VB so its like night and day ha )

    David

  2. #2
    Join Date
    Aug 2008
    Posts
    114

    Re: c# access variable from other namespaces

    Had to do this:
    Code:
    public static string cableOrSat = "CABLE";
    David

  3. #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.

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

    Re: c# access variable from other namespaces

    Didn't see that 2nd post...
    That's OK if you want cableOrSat to be static, however, then you can't have several instances with different cableOrSat - they'll all share the same field. In fact, VB.NET equivalent to static keyword is the Shared keyword.

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