/// <summary> /// updates a property for a child /// </summary> /// <param name="c">the child to update</param> /// <param name="prop">the property to update (if it is SpecialNeeds, it will ADD the new value to the list)</param> /// <param name="newVal">the new value for the property</param> /// <exception cref="System.Exception">thrown if the new value is not of a proper type for the property</exception> public void UpdateChild(Child c, Child.Props prop, object newVal) {//**** //if (prop == Child.Props.BirthDate) //{ // if (!(newVal) //} // throw new Exception("Age cannot be below 0"); dal.UpdateChild(c, prop, newVal); }
public void UpdateChild(Child c, Child.Props prop, object newVal) { LoadData(TypeToLoad.Child); XElement ChildToUpdate = (from ch in ChildrenRoot.Elements() where ch.Element("ID").Value == c.ID select ch).FirstOrDefault(); switch (prop) { //case Child.Props.BirthDate: // if (newVal is DateTime) // c.BirthDate = (DateTime)newVal; // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Child.Props.FirstName: if (newVal is string) { ChildToUpdate.Element("FirstName").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Child.Props.HaveSpecialNeeds: if (newVal is bool) { ChildToUpdate.Element("HaveSpecialNeeds").Value = newVal.ToString(); if (!(bool)newVal) { ChildToUpdate.Element("SpecialNeeds").RemoveAll(); // if the child has no special needs, that means the list of them makes no sense } } else { throw new Exception("wrong type for the 3rd parameter"); } break; //case Child.Props.ID: // if (newVal is string) // { // foreach (Nanny ch in DataSource.Nannies) // if (ch.ID == c.ID) // throw new Exception("ID already exists!"); // foreach (Child ch in DataSource.Children) // if (ch != c && ch.ID == c.ID) // throw new Exception("ID already exists!"); // foreach (Mother ch in DataSource.Mothers) // if (ch.ID == c.ID) // throw new Exception("ID already exists!"); // c.ID = (string)newVal; // } // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Child.Props.LastName: if (newVal is string) { ChildToUpdate.Element("LastName").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Child.Props.MotherID: if (newVal is string) { foreach (Mother ch in GetMothers()) { if (ch.ID == (string)newVal) { ChildToUpdate.Element("MotherID").Value = (string)newVal; break; } else { throw new Exception("mother doesn't exist"); } } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Child.Props.SpecialNeeds: if (newVal is string) { ChildToUpdate.Element("SpecialNeeds").Add(new XElement("Need", (string)newVal)); ChildToUpdate.Element("HaveSpecialNeeds").Value = "true"; // else the added speical need makes no sense. Note that this is actually a kind of "explicit binding" between the 2 fields } else { throw new Exception("wrong type for the 3rd parameter"); } break; } ChildrenRoot.Save(Address + "\\Children.xml"); }
/// <summary> /// updates a property for a child /// </summary> /// <param name="c">the child to update</param> /// <param name="prop">the property to update (if it is SpecialNeeds, it will ADD the new value to the list)</param> /// <param name="newVal">the new value for the property</param> /// <exception cref="System.Exception">thrown if the new value is not of a proper type for the property</exception> public void UpdateChild(Child c, Child.Props prop, object newVal) { switch (prop) { //case Child.Props.BirthDate: // if (newVal is DateTime) // c.BirthDate = (DateTime)newVal; // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Child.Props.FirstName: if (newVal is string) { c.FirstName = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Child.Props.HaveSpecialNeeds: if (newVal is bool) { c.HaveSpecialNeeds = (bool)newVal; if (!c.HaveSpecialNeeds) { c.SpecialNeeds.Clear(); // if the child has no special needs, that means the list of them makes no sense } } else { throw new Exception("wrong type for the 3rd parameter"); } break; //case Child.Props.ID: // if (newVal is string) // { // foreach (Nanny ch in DataSource.Nannies) // if (ch.ID == c.ID) // throw new Exception("ID already exists!"); // foreach (Child ch in DataSource.Children) // if (ch != c && ch.ID == c.ID) // throw new Exception("ID already exists!"); // foreach (Mother ch in DataSource.Mothers) // if (ch.ID == c.ID) // throw new Exception("ID already exists!"); // c.ID = (string)newVal; // } // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Child.Props.LastName: if (newVal is string) { c.LastName = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Child.Props.MotherID: if (newVal is string) { foreach (Mother ch in DataSource.Mothers) { if (ch.ID == (string)newVal) { c.MotherID = (string)newVal; break; } else { throw new Exception("mother doesn't exist"); } } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Child.Props.SpecialNeeds: if (newVal is string) { c.SpecialNeeds.Add((string)newVal); c.HaveSpecialNeeds = true; // else the added speical need makes no sense. Note that this is actually a kind of "explicit binding" between the 2 fields } else { throw new Exception("wrong type for the 3rd parameter"); } break; } }