public ValueContainer Evaluate(IValueEnvironment env)
{
//TODO: Make it so no casting is needed
DictionaryKeyValueObserver<string, ValueContainer> repeatVariables = ((Environment.ValueEnvironment)env).GetRange(Name);
ValueContainer value = new ValueContainer(0);
Action onValueChanged = () =>
{
int count = 0;
double sum = 0;
foreach (ValueContainer item in repeatVariables.Where(v => !v.Inactive))
{
count++;
sum += Convert.ToDouble(item.Value);
}
value.Value = count > 0 ? sum / count : 0;
};
Action onCollectionChange = () =>
{
foreach (ValueContainer item in repeatVariables)
{
item.ValueChanged += onValueChanged;
}
onValueChanged();
};
repeatVariables.FilterResultChanged += onCollectionChange;
onCollectionChange();
return value;
}