Aborting a constructor

  • Thread starter Thread starter Marc Scheuner [MVP ADSI]
  • Start date Start date
M

Marc Scheuner [MVP ADSI]

Folks,

I have a plug-in system where I have a host that creates child forms
to be displayed (in an MDI manner). One requirement is that if certain
conditions apply, a child form should not be created, e.g. I would
have to be able to abort the child's constructor, if I see that
certain things are missing.

How can I do this? I come from Delphi, and we used to have a "silent"
exception called "Abort" there - throw it in the constructor and the
execution of the constructor was stopped right there, and a null
pointer was returned (instead of a created instance) - is there
something similar in C# ? Can't seem to find anything to that
effect.....

Thanks!
Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Marc Scheuner said:
I have a plug-in system where I have a host that creates child forms
to be displayed (in an MDI manner). One requirement is that if certain
conditions apply, a child form should not be created, e.g. I would
have to be able to abort the child's constructor, if I see that
certain things are missing.

How can I do this? I come from Delphi, and we used to have a "silent"
exception called "Abort" there - throw it in the constructor and the
execution of the constructor was stopped right there, and a null
pointer was returned (instead of a created instance) - is there
something similar in C# ? Can't seem to find anything to that
effect.....

The equivalent is to have a static method which calls a constructor and
either returns the new reference or null if an exception occurs. I for
one am very glad that constructors can't silently return null!
 
Marc,
To further Jon's comment.

I would considering having the static method return a NullObject instead of
null. Depending on how far & wide the returned object was going to be used.
This avoids checking for null all over your code...

A NullObject is an application of the Special Case pattern.
http://www.martinfowler.com/eaaCatalog/specialCase.html

Hope this helps
Jay
 
Back
Top