tags:

views:

143

answers:

5

Here is the code that demonstrates my problem (All in the same namespace):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Fubar.BGroup.A = true;
    }

    public Foo Fubar = new Foo();
}

public class Foo
{
    public Foo()
    {
    }

    private BoolGroup bGroup = new BoolGroup();
    public BoolGroup BGroup
    {
        get{ return this.bGroup; }
        set
        { 
            this.bGroup = value;
            this.doSomething();
        }
    }
}

public class BoolGroup
{
    public BoolGroup()
    {
    }

    private bool a;
    public bool A
    {
        get { return this.a; }
        set { this.a = value; }
    }
}

private void doSomething()
{
    ....
}

I will never get to doSomething() and I really want to. What am I doing wrong? The values will all get set properly, but I never seem to get into that the set part of BGroup.

Thanks

+3  A: 

In your code, you're never setting BGroup. You're setting A, which is a property of BGroup.

Try something like

this.Fubar.BGroup = new BoolGroup();
Larsenal
+3  A: 

You never set BGroup at all. The closest things you do are Fubar.BGroup.A = true and bGroup = new BoolGroup();.
Fubar.BGroup.A = true gets the BGroup property, and sets the A property on the BoolGroup object, it doesn't set the BGroup.
bGroup = new BoolGroup() sets the backing field of the BGroup property, which is why you get that BoolGroup when you get BGroup, but it doesn't go through the setter.

If you want to use the setter, your Foo class should be like this:

public class Foo
{
    public Foo()
    {
        // Note uppercase on BGroup to access the property and 
        //   not its backing field.
        BGroup = new BoolGroup(); 
    }

    private BoolGroup bGroup;
    public BoolGroup BGroup
    {
        get{ return this.bGroup; }
        set
        { 
            this.bGroup = value;
            this.doSomething();
        }
    }
}
Joren
All of the answers solved the problem, but this one explained it the best to me. Thanks to all.
EatATaco
A: 

You are not calling the setter of the BGroup property with this statement:

this.Fubar.BGroup.A = true;

"this.Fubar.BGroup" calls the getter (returns a BoolGroup) and with ".A = true" you are calling the setter of the A property of BoolGroup.

M4N
A: 
this.Fubar.BGroup.A = true;

is setting the property A of Fubar.BGroup, it's not setting Fubar.BGroup.

najmeddine
A: 

Your code looks fine. The reason it isn't triggering is because you aren't actually setting Fubar.BGroup anywhere. You are setting Fubar.BGroup.A in the Form constructor....this won't trigger the doSomething method. You need to do something like:

this.Fubar.BGroup = new BGroup();

or do it internally in your Foo constructor:

public Foo()
{
    this.BGroup = new BGroup();
}
James
Your first suggestion is fine, but your second suggestion is basically the same as what he's already doing. Did you mean `this.BGroup = new BGroup();`?
Joren
Sorry yes that was supposed to be the property not the private field I was assigning. I will update.
James