tshad wrote:
>How do you create a null object if the Constructor finds an error and thus
>would make the object invalid?
>
>If I have a Class Role and during the constructer there is something
>wrong, I want to do something like the following:
>
> public Role()
> {
> connectionString = ConfigurationSettings.AppSettings["x"];
> if (connectionString == "")
> Don't create the object and pass back a null
> }
In addition to the other suggestions - this looks like a situation to me,
where you actually detect an invalid state during object construction. In
that case, I would probably throw an exception in the constructor.
I see the distinguishing factors like this: if the condition is part of
your normal application flow logic, you'll want to detect and handle it
without exceptions - but you need to do that "from the outside", meaning
not from the constructor. A factory method may be fine for that. But if
the condition is something that shouldn't occur unless something went
wrong, I'd rather throw an exception from the constructor - no need to
introduce additional management code in the form of factory methods or
other "outside" checks for this error situation.
Oliver Sturm
--
Expert programming and consulting services available
See
http://www.sturmnet.org (try /blog as well)