Access properties in a different class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Access properties in a different class

  1. #1
    Join Date
    Apr 2011
    Posts
    58

    Access properties in a different class

    Let's say I have this code:

    Code:
    using this;
    using that;
    
    namespace myNamespace
    {
      public partial class Form1 : Form
      {
        public string myString;
    
        public Form1()
        {
          InitializeComponent();
        }
        
        private void SetButton_Click(object sender, EventArgs e)
        {
          MyOtherClass.TryToAccessMyString()
        }
      }
    }
    And then in MyOtherClass...

    Code:
    public class MyOtherClass
    {
      internal static void TryToAccessMyString()
      {
        string testString = myNamespace.Form1.myString; //doesn't work
      }
    }
    Isn't there a way to access myString without passing it as an argument? It's been ten+ years since I have taken a programming class, so I am a little rusty. Please go easy on me. What is this concept called that I am trying to do (or break?) I'd be happy to just read up on it but I don't even know what to look up.

    Thanks in advance.

    Skip

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Access properties in a different class

    It "doesn't work" because you are trying to access it as if it were a static field. It is not; it is a member variable, i.e., a separate instance of that string belongs to each separate instance of your Form1 class. You will (and should) pass it as an argument to avoid passing references to your main form around all over the place.

  3. #3
    Join Date
    Apr 2011
    Posts
    58

    Re: Access properties in a different class

    BigEd781, thanks.
    So even if I have eight or ten member variables in my main form, I should still pass them to other classes (generally by value). That just seems like a lot of variable passing. I should probably consider putting all those variables in a class by themselves, huh? Or is there a better way?
    Let's say I have a checkbox on the form to determine whether my application should keep a log in a text file of different events, perhaps to be used as troubleshooting. Every class I use will want access to that variable to know whether or not to log its methods and events. I can certainly pass that setting to each class, but to me if just seemed neater if I could just look back and see its value. How would you handle this?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Access properties in a different class

    Quote Originally Posted by MrGibbage View Post
    BigEd781, thanks.
    So even if I have eight or ten member variables in my main form, I should still pass them to other classes (generally by value). That just seems like a lot of variable passing. I should probably consider putting all those variables in a class by themselves, huh? Or is there a better way?
    Let's say I have a checkbox on the form to determine whether my application should keep a log in a text file of different events, perhaps to be used as troubleshooting. Every class I use will want access to that variable to know whether or not to log its methods and events. I can certainly pass that setting to each class, but to me if just seemed neater if I could just look back and see its value. How would you handle this?
    Well, like most things, it depends.

    For your checkbox example I would likely use a static field in your logging class. For example, something similar to this:

    Code:
    class SomeLoggerClass
    {
        public static bool IsLoggingEnabled { get; set; }
    
        public static void Log( string message )
        {
            if( IsLoggingEnabled )
            {
                // log here
            }
        }
    }
    Obviously that is simplified, and you may find that a different scheme works better in your application, but this way you don't need to check if logging is enabled throughout your code base as it is completely encapsulated by the logging class. You need only set IsLoggingEnabled once and you just use the Log method throughout your code.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center