M
Mark Broadbent
I agree, I'd thought about the possibility of throwing a custom Exception,
and it is probably the most sensible way to go with this design.
Cheers,
Mark.
and it is probably the most sensible way to go with this design.
Cheers,
Mark.
Bill Butler said:So really it makes sense what is happening because obviously a null
reference cannot have a Remove method, but the compiler does not know a
Remove method is being called on a null reference till runtime.
You really need to decide how to handle this in a way that makes the most
sense for your application.
You could check for a null Item before calling Remove()
You could add a try block
Returning an empty Item is probably not a good idea unless you check for
it.
Other wise you can have this:
Item item = basket.Items["spannerintheworks"]; //returns the empty
item;
Func(item); //use it like it was perfectly good
This is probably a bad idea.
perhaps an exception in the case of
basket.Items["spannerintheworks"].Remove();
is a good idea, since you don't actually have the item object.
If you don't actually have the object
basket.Items.Remove(["spannerintheworks");
makes more sense AND it won't throw an exception.
Thanks for the time you took on this code and your comments.
No problem
Bill