I have a composite property called Items of type List to an order class. On the GUI the user fills out some fields like Name, Description, Price, Quantity, etc… and then clicks the Add Item button which of course adds the item to the order’s list of items. What I’d like to do is create a method that checks the item’s IsComplete property which does a check to ensure the required properties are set so that way someone can’t just call order.Items.Add(item) if it isn’t complete. If it’s not I’d like an exception to be thrown if the item’s IsComplete property returns false… What would be an easy way to go about this?
Share
This can be achieved by sub-classing
List<T>into a derived class, and then overriding theAddmethod, like so.Your order class would then have the property
MyItemCollectionrather thanList<T>, like so: