public override void Set(AbstractSymbolValueProvider vp, ISymbolValue value) { var oldV = vp[Variable]; if (oldV is AssociativeArrayValue) { if (Key != null) { var aa = (AssociativeArrayValue)oldV; int itemToReplace = -1; for (int i = 0; i < aa.Elements.Count; i++) { if (SymbolValueComparer.IsEqual(aa.Elements[i].Key, Key)) { itemToReplace = i; break; } } // If we haven't found a matching key, add it to the array var newElements = new KeyValuePair <ISymbolValue, ISymbolValue> [aa.Elements.Count + (itemToReplace == -1 ? 1: 0)]; aa.Elements.CopyTo(newElements, 0); if (itemToReplace != -1) { newElements[itemToReplace] = new KeyValuePair <ISymbolValue, ISymbolValue>(newElements[itemToReplace].Key, value); } else { newElements[newElements.Length - 1] = new KeyValuePair <ISymbolValue, ISymbolValue>(Key, value); } // Finally, make a new associative array containing the new elements vp[Variable] = new AssociativeArrayValue(aa.RepresentedType as AssocArrayType, newElements); } else { if (vp.ev != null) { vp.ev.EvalError(null, "Key expression must not be null", Key); } } } else { if (vp.ev != null) { vp.ev.EvalError(null, "Type of accessed item must be an associative array", oldV); } } }
public override void Set(AbstractSymbolValueProvider vp, ISymbolValue value) { var oldV = vp[Variable]; if (oldV is AssociativeArrayValue) { if (Key != null) { var aa = (AssociativeArrayValue)oldV; int itemToReplace = -1; for (int i = 0; i < aa.Elements.Count; i++) if (SymbolValueComparer.IsEqual(aa.Elements[i].Key, Key)) { itemToReplace = i; break; } // If we haven't found a matching key, add it to the array var newElements = new KeyValuePair<ISymbolValue, ISymbolValue>[aa.Elements.Count + (itemToReplace == -1 ? 1 : 0)]; aa.Elements.CopyTo(newElements, 0); if (itemToReplace != -1) newElements[itemToReplace] = new KeyValuePair<ISymbolValue, ISymbolValue>(newElements[itemToReplace].Key, value); else newElements[newElements.Length - 1] = new KeyValuePair<ISymbolValue, ISymbolValue>(Key, value); // Finally, make a new associative array containing the new elements vp[Variable] = new AssociativeArrayValue(aa.RepresentedType as AssocArrayType, newElements); } else{ if(vp.ev !=null) vp.ev.EvalError(null,"Key expression must not be null", Key); } } else{ if(vp.ev != null) vp.ev.EvalError(null,"Type of accessed item must be an associative array", oldV); } }