0

Imagine I have this class:

Class Foo
{
    public Bar b1 { get; set; }
    public Bar b2 { get; set; }
    public Bar b3 { get; set; }

    public void UpdateBarsMyProp(bool value)
    {
        // ????
    }
}

Class Bar
{
    public bool MyProp { get; set; }

    public bool UpdateMyProp(bool value)
    {
        this.MyProp = value;
    }
}

What's the best way to update the property MyProp in b1, b2 and b3?

Generics?

Delegates?

EDIT:

Just to add more information about my specific situation:

I'm creating a virtual keyboard and I'm using WPF MVVM so I have:

A KeyBoard ViewModel which contains several Key ViewModels, I can't store them in a List because im my View (xaml file) I need to bind each key information to a specific ViewModel.

Now, when the user presses the virtual shift button, I need my Keyboard ViewModel object to update the display char in every Key ViewModel.

davioooh
  • 23,742
  • 39
  • 159
  • 250
Eduardo Brites
  • 4,597
  • 5
  • 36
  • 51

4 Answers4

2

You could put your properties in a List<Bar> (or an array if you prefer...) and iterate over it.

So:

public Bar b1 { get; set; }
public Bar b2 { get; set; }
public Bar b3 { get; set; }
// other Bar props...

private List<Bar> barsList = new List<Bar>(){ b1, b2, b3, ... };

public void UpdateBarsMyProp(bool value)
{
    foreach(Bar bar in barsList)
    {
        bar.MyProp = value;
    }
}
davioooh
  • 23,742
  • 39
  • 159
  • 250
  • I guess I'll need to store them in a List just because of this requirement. – Eduardo Brites Aug 31 '12 at 10:24
  • I'm not saying to remove `Bar` properties. You can maintain them in your class to bind to xaml controls. however you can also have a private list/array of Bars that contains all your properties references. so you can easily assign common values to them – davioooh Aug 31 '12 at 10:28
  • @EduardoBrites but you'll have to maintian the bar properties for enternity why not bind to a public indexer? – Jodrell Aug 31 '12 at 10:45
0

Maybe your example is simplfied, but why not just do

b1.MyProp = b2.MyProp = b3.MyProp = value;

Also, why bother with the UpdateMyProp method? This is the same as the property setter method you have. If you need to add more logic to the setter, you can stop using the auto-implemented property by changing

public bool MyProp { get; set; }

to

private bool myProp;

public bool MyProp
{
   get { return this.myProp; }
   set
   {
      // any logic here
      this.myProp = value;
   }
}
Graham Clark
  • 12,886
  • 8
  • 50
  • 82
0

If all your bar objects require the same MyProp, you can set MyProp as static:

public static bool MyProp { get; set; }

You can then edit all MyProps of all bar objects with:

Bar.MyProp = baz;

Only use this if ALL the Bar objects share the same MyProp

Ossie7
  • 360
  • 3
  • 8
0

You maybe want somthing like this.

class Foo
{
    private readonly IList<Bar> bars = new List<Bar>
        {
            new Bar(),
            new Bar(),
            new Bar()
        }

    public Bar this[int i]
    {
        get
        {
           return this.bars[i];
        }
    }

    public void UpdateBars(bool value)
    {
        foreach (var bar in this.bars)
        {
            bar.MyProp = value;
        }
    }
}

You could then access the first bar like this

var foo = new Foo();
var firstBar = foo[0];

You can bind to a indexer with a little convertor, this would make your model less brittle.


if you didn't want to use an indexer, you could raise a setter to Foo.

Class Foo
{
    public Bar b1 { get; set; }
    public Bar b2 { get; set; }
    public Bar b3 { get; set; }

    public bool MyProp
    {
        set
        {
            if (this.b1 != null)
            {
                this.b1.MyProp = value;
            }

            if (this.b2 != null)
            {
                this.b2.MyProp = value;
            }

            if (this.b3 != null)
            {
                this.b3.MyProp = value;
            }
        }
    }
}
Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124