vb.net - Chaining overloaded constructors -


i trying create efficient class minimum code-duplication.

i have defined:

public class foo     private _firstname string = ""     private _lastname string = ""      public sub new(byval userguid guid)         'query db firstname , lastname         me.new(dt.rows(0)("firstname").tostring(),dt.rows(0)("lastname").tostring())     end sub      public sub new(byval firstname string, byval lastname string)         _firstname = firstname.toupper()         _lastname = lastname.toupper()         validate()     end sub      private sub validate()         ' throw error if wrong     end sub end class 

the constructor firstname , lastname parameters end-point constructor validation. constructor userguid parameter query db obtain name , call final constructor. way execution should directed towards 1 of constructors validation etc etc. idea behind if add new contructors, have pull necessary data (firstname/lastname) , call final constructor validation.

however, there compilation error preventing me using system on line me.new(dt.rows(0)("firstname").tostring(),dt.rows(0)("lastname").tostring()). apparently line has first line in constructor. if have first line, break validation process because validation throw error due no firstname/lastname. have query db in order pull info.

i aware can assign values here , call validation constructor too, isolate constructor final one, duplicating code , adding maintenance bit. fyi, in example below have 2 constructors, in reality have several more. if each own assignment adds maintenance much.

so, there way achieve task executing code , calling overloaded constructor?

thank insight

update 1:

per the_lotus comment, including dt definition. there workaround issue. take validation , assignment out of final constructor , put function. constructors call function, eliminating need chain constructors. doesn't bad, understand why in order chain constructors have put constructor calls on first line.

here new code:

public class foo private _firstname string = "" private _lastname string = ""

public sub new(byval userguid guid)     dim dt new datatable     ' query db firstname , lastname     ' assume populate dt @ least 1 datarow     assignandvalidate(dt.rows(0)("firstname").tostring(), dt.rows(0)("lastname").tostring())     'me.new(dt.rows(0)("firstname").tostring(), dt.rows(0)("lastname").tostring()) end sub  public sub new(byval firstname string, byval lastname string)     assignandvalidate(firstname, lastname) end sub  private sub validate()     ' throw error if wrong end sub  private sub assignandvalidate(byval firstname string, byval lastname string)     _firstname = firstname.toupper()     _lastname = lastname.toupper()     validate() end sub 

end class

one curious not mention: online code converters (vb.net c#) have no issues converting chained constructor calls not on first line. c# code comes this.#ctor(dt.rows(0)("firstname").tostring(), dt.rows(0)("lastname").tostring()); however, if try convert vb.net, fails.

what looking factory method

public class foo       public shared function getfoofromguid(byval userguid guid) foo          ' query db          return new foo(dt.rows(0)("firstname").tostring(), dt.rows(0)("lastname").tostring())     end function  end class 

or initialization function

public class foo       public sub new(byval userguid guid)         ' query db firstname , lastname         initialize(dt.rows(0)("firstname").tostring(), dt.rows(0)("lastname").tostring())     end sub      public sub new(byval firstname string, byval lastname string)         initialize(firstname, lastname)     end sub      private sub initialize(byval firstname string, byval lastname string)     end sub  end class 

personally, wouldn't call database inside new.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -