views:

11

answers:

1

I have a Class similar to the following:

public delegate void FanChangedEventHandler(Fan sender, EventArgs e);

public class Fan: BindableProperty
{   
    #region Blade parameters
    //These Parameters apply to individual blades but are the same for each blade.

    private double length;
    public double Length
    {
        get
        {
            return length;
        }
        set
        {
            length = value;
            NotifyPropertyChanged("Length");
            OnFanChanged(EventArgs.Empty);
        }
    }

    #endregion

    #region Preliminary Fan Outputs
    public double thrust
    {
        get
        {
            return GetThrust();
        }
    }
    private double GetThrust()
    {
        double ThrustSum = 0;
        for(int i = 0; i < Blades.count; i++)
        {
              ThrustSum += Blades[i].Thrust;
        }

        return ThrustSum;
    }
    #endregion

    #region Final Fan Outputs
    public double FinalThrust
    {
        get
        {
            return GetFinalThrust();
        }
    }
    private double GetFinalThrust()
    {
        double TotalWind = 0;
        for(int i = 0; i < Winds.count;i++)
        {
             TotalWind += Winds[i];
        }

        return Thrust + TotalWind;
    }
    #endregion

    private List<Blade> blades;
    public List<Blade> Blades
    {
        get
        {
            return blades;
        }
        set
        {
            blades = value;
            NotifyPropertyChanged("Blades");
        }
    }

    private List<Wind> winds;
    public List<Wind> Winds
    {
        get
        {
            return winds;
        }
        set
        {
            winds = value;
            NotifyPropertyChanged("Winds");
        }
    }

    public event FanChangedEventHandler FanChanged;
    protected virtual void OnFanChanged(EventArgs e)
    {
        try
        {
            UpdateProperties();

            //This seems verbose, but it is important.
            //http://blogs.msdn.com/csharpfaq/archive/2004/03/19/93082.aspx
            FanChangedEventHandler handler = FanChanged;

            if (handler != null)
                handler(this, e);
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
    }

    public Fan()
    {
        Length = 21.75;

        Blades = new List<Blade>();
        for (int i = 0; i < 5; i++)
        {
            Blades.Add(new Blade(this));
            Blades[i].BladeChanged += new BladeChangedEventHandler(Blade_Changed);
        }

        Winds = new List<Wind>();
        for (int i = 0; i < 3; i++)
        {
            Winds.Add(new Wind());
            RegisterWind(Winds[i]);
        }
    }

    public void RegisterWind(Wind wind)
    {
        wind.WindChanged += new WindChangedEventHandler(Wind_Changed);
    }

    private void Blade_Changed(Blade sender, EventArgs e)
    {
        UpdateProperties();
    }

    private void Wind_Changed(Wind sender, EventArgs e)
    {
        UpdateProperties();
    }

    private void UpdateProperties()
    {
            NotifyPropertyChanged("Thrust");
            NotifyPropertyChanged("FinalThrust");
    }
}

So, if you change the length parameter it triggers an event. This event notifies all blades that they should update themselves, the blades update themselves and then notify when they have changed and the fan in turn updates itself.

I'm running into a few problems, one is that if I change Length, Fan will be updated five times when it only needs to be updates once.

Another is I'm not sure if the FinalThrust will always be calculated in the right order. First the Blades must update, then Thrust and the winds must be updated, and after that finalThrust can be calculated. (I have checked the results from my actual class and they are correct right now, but I do not know if this could cause bugs in the future)

Finally, I would like to optimize Fan, which means putting it into a loop. How can I tell if all updates are complete so that I can check the output values and make my next optimization iteration?

I've asked questions before leading to this and the answer was I should create a state machine, but I don't know how I would incorporate a state machine into this.

I am definitely more engineer than programmer! thanks for the help :)

A: 

You could fire an event from your Length update. This could increment a counter that when it hits 5 (or however many blades there are on the fan) updates the fan.

This in turn fires an event which the FinalThrust update subscribes to. Again if this requires other preconditions it could set a flag and the update only happens when all flags are set.

You can continue this chaining firing an event out of the final link to indicate that everything is complete and the next iteration can commence.

ChrisF