-
May 9th, 2011, 08:40 PM
#1
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
-
May 10th, 2011, 01:17 AM
#2
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.
-
May 10th, 2011, 05:20 AM
#3
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?
-
May 10th, 2011, 11:16 AM
#4
Re: Access properties in a different class
Originally Posted by MrGibbage
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|