c# - How to avoid copy-paste of exceptionness function? -


i have following functionality in few different classes c1, d2, f34:

class c1 {     void somefunc     {         statement1();         obj = getobj();         statement2(obj);     }     imyobj obj{get;private set;} }  public static class myobjext {     public static void statement2(this imyobj obj)     {         ... validation of 'obj';         ... throw exception if object state wrong     } } 

classes c1, d2, f34 aren't member of same hierarchy.

so avoid copy paste of them.

i this:

static myobj myfunc() {   statement1();   imyobj obj = getobj();   statement2(obj);    return obj; } class c1 {   void somefunc   {       obj = myfunc();   }   imyobj obj{get;private set;} } 

but if "statement2" function throws exception obj member left uninitialized...

how avoid copy-paste?

i have little bit more complicated solution:

class extender {     static imyobj myfunc(out imyobj obj)     {         statement1();         obj = getobj();         statement2(obj);     } }  class c1 {   void somefunc   {     imyobj obj=null;     try     {          myfunc(out obj);     }         {         obj = obj;     }   }   imyobj obj{get;private set;} } 

but not sure if , must work. or bad approach?

if think - please vote, if no - please point "why"?

thanks lot!

edit: added 'somefunc' implementation after modification.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -