1

i've often had this issue where i do not really understand how to pass userform variables into classes. for example i have a button:

private void button1_Click(object sender, EventArgs e)
{
    DoStuff();
}

and a method in the form class:

DoStuff()
{

    Class123 myclass = new Class123();

}


...
...

class Class123
{

    //how do i pass for example in myotherClass whether or not my checkbox on the userform is checked? i dont want to have to pass from method to method to class to class. what is the logical/smart way of handling this?
    ClassData myotherClass = new ClassData();
}

how do i pass for example in myotherClass whether or not my checkbox on the userform is checked? i dont want to have to pass from method to method to class to class. what is the logical/smart way of handling this?

K Mehta
  • 10,323
  • 4
  • 46
  • 76
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

3 Answers3

3

I think you are looking for function arguments:

// notice the declared function argument isMyCheckboxChecked
DoStuff(bool isMyCheckboxChecked)
{
    Class123 myclass = new Class123(isMyCheckboxChecked);

}

private void button1_Click(object sender, EventArgs e)
{
    // passing the state of the checkbox to DoStuff as an argument
    DoStuff(chkMyCheckbox.Checked);
}


class Class123
{
     readonly ClassData myotherClass = new ClassData();

     Class123(bool isMyCheckboxChecked) 
     { 
          myOtherClass.isMyCheckboxChecked = isMyCheckboxChecked;
     }
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • yes but then i am passing that stupid variable across everything – Alex Gordon Apr 05 '12 at 23:13
  • Which is way better than just letting everybody touch ---your private parts--- global data. Also, I thought your sample looks a bit over-complicated, it could probably do without fewer classes, as far as I can tell. Also, DoStuff could be a member on Class123 directly – sehe Apr 05 '12 at 23:16
  • 1
    Readonly means that you cannot assign to the (reference) member myotherClass. You can still modify the instance, but it will remain the same instance. – sehe Apr 06 '12 at 08:13
2

I can see a few things here. The code posted is rather vague, so it is hard to say what the correct answer may be.

  1. If myOtherClass needs to know if a checkbox is checked when the checkbox changes then you should probably look into using a subscriber pattern.

  2. However, if you mean that you just need to know if the checkbox was checked at the moment DoStuff() ran, there is nothing wrong about passing a variable. In fact, passing a variable is the preferred way - it's what variables exist for. That said, you need to pass variables intelligently; if you find that you are just slinging parameters across classes constantly, that's a sign of poorly-designed code. If you need to pass some parameters to myClass to tell it what to do, build them into a (descriptively named) class of their own, and pass that class to myClass's constructor instead of a long list of parameters.

SouthShoreAK
  • 4,176
  • 2
  • 26
  • 48
  • Here are some examples: http://msdn.microsoft.com/en-us/library/dd783449.aspx http://msdn.microsoft.com/en-us/library/ee817669.aspx – SouthShoreAK Apr 06 '12 at 04:01
  • does that mean you dont know? – Alex Gordon Apr 06 '12 at 05:11
  • @I__ you know, that isn't really funny. Are you trying to get help, or just having a good time? You might have a look at http://chat.stackoverflow.com/ – sehe Apr 06 '12 at 08:12
1

I disagree with this approach.
Any 'smart' method, if it even exist, will break the golden rules of Object Oriented Programming. An object is a self contained item of data that can only be accessed or changed in a controlled way. This prevents side effects, a common problem in procedural code, where data is globally accessible. In OOP, the objects can receive or send messages to other objects only by calling their methods.

EDIT: To show a way to do it

public static class MyApp
{
    public static bool MyCheckBox {get; set;}
}

in your doStuff

MyApp.MyCheckBox = this.checkBox1.Checked;

inside a method of your myOtherClass

   if(MyApp.MyCheckBox == true)
   ...

this is the same as using a global variable in the old days of procedural languages. This paves the way to difficult to track bugs and creates state mode that render an application hard to maintain

Steve
  • 213,761
  • 22
  • 232
  • 286
  • fine, what is a dumb way to do this ? – Alex Gordon Apr 05 '12 at 22:57
  • 1
    All right. Create a global static class with a boolean property representing the state of your checkbox. Set the value in your DoStuff and read everywhere you have access to your global static class. – Steve Apr 05 '12 at 23:00
  • 1
    I'm sorry, but I don't agree. Basically you just told him to create a cleverly-wrapped global variable. – SouthShoreAK Apr 05 '12 at 23:03
  • what would be your solution mr @SouthShoreAK – Alex Gordon Apr 05 '12 at 23:03
  • @steve awesome thanks so much. how do i assign a default value for MyCheckBox? – Alex Gordon Apr 05 '12 at 23:08
  • I did, because that comment suggests very poor programming practice. "A global static class with a boolean property" is essentially a global variable, which is almost universally frowned upon. – SouthShoreAK Apr 05 '12 at 23:09
  • @SouthShoreAK yes exactly what i have said. It's a global variable in disguise. And I have also told of the many reasons why you should not do this. If you downvoted, fine, but the fact that this code is possible doesn't mean that I reccommend it. – Steve Apr 05 '12 at 23:11
  • @steve awesome thanks so much. how do i assign a default value for MyCheckBox? – Alex Gordon Apr 05 '12 at 23:12
  • You could set the value of MyCheckBox at the start of your program or into the constructor of your form. Static class can be used immediatly, at the first line of code of your program. – Steve Apr 05 '12 at 23:16
  • @Steve, thanks for the edit. It wasn't initially clear that you agreed it wasn't a good thing to do. – SouthShoreAK Apr 05 '12 at 23:19
  • I reccommend to everyone debating this issue to look [at this previous question](http://stackoverflow.com/questions/576853/what-is-the-use-of-a-static-class) on this board. That question puts more context to the pros and cons of static classes. – Steve Apr 06 '12 at 07:27