Aborting a constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Scheuner [MVP ADSI]

    #1

    Aborting a constructor

    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)i nova.ch
  • Jon Skeet

    #2
    Re: Aborting a constructor

    Marc Scheuner [MVP ADSI] <m.scheuner@ino va.SPAMBEGONE.c h> wrote:[color=blue]
    > 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.....[/color]

    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!

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Aborting a constructor

      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.
      A subclass that provides special behavior for particular cases.


      Hope this helps
      Jay

      "Marc Scheuner [MVP ADSI]" <m.scheuner@ino va.SPAMBEGONE.c h> wrote in message
      news:km9rkvch2t 336gjpmeokou6pp ekoivkk2v@4ax.c om...[color=blue]
      > 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)i nova.ch[/color]


      Comment

      Working...