Question

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

Was it helpful?

Solution

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();
        }
    }
}

OTHER TIPS

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();

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.

this.Fubar.BGroup.A = true;

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

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top