Results 1 to 10 of 10

Thread: [02/03] calling constructor within a constructor

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Exclamation [02/03] calling constructor within a constructor

    can we call constructor within a constructor??

    i mean; i've large no of attributes which i've to set through constructor. the list of these attributes in one constructor becomes very large. so can i call constructor within constructor?


    like

    VB Code:
    1. Sub New (ByVal id As Integer, ByVal name As string, ByVal charges As itemCharges)
    2.  
    3. End Sub

    or any other way to do it?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [02/03] calling constructor within a constructor

    Not sure what you are after. Doesn't sound very elegant.

    Can you elaborate on the problem?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Re: [02/03] calling constructor within a constructor

    Quote Originally Posted by penagate
    Not sure what you are after. Doesn't sound very elegant.

    Can you elaborate on the problem?

    look, i've to declare so many properties for a class like


    Sub New(ByVal id As Integer, ByVal name As string, ByVal address as string, ByVal phone As Integer, ByVal city As string, ByVal country As string, ByVal itemCode As string, ByVal modelNo, and so on...........)

    the list of these properties/attributes is very long may be upto 35. it doesn't seem so elegant that's i 'm asking so

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] calling constructor within a constructor

    Constructors are just methods so you can call them from each other like any other method, except that you would only ever call one other constructor and you would always do it as the first operation. It is preferable to have an overloaded constructor and have the more basic overloads pass default values to the more complex ones rather than have a single constructor with optional parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [02/03] calling constructor within a constructor

    Address, City, Country - you could put them in a "Location" struct.

    VB Code:
    1. Public Type LocationStruct
    2.   Address As String
    3.   City As String
    4.   Country As String
    5. End Type

    And you can then pass that to the constructor:
    VB Code:
    1. Sub New(... ByVal location As LocationStruct ...)

    And make sure that every parameter really needs to be set in the constructor - or could it be made a property instead.

    You can also use optional arguments, with defaults.
    VB Code:
    1. Sub New(... Optional ByVal something As String = "Default Value" ...)
    (I think that's how you do it)

    and finally there is method overloading by which you can make different versions of the constructor with different numbers of arguments, so the right one will be called depending on the number and datatype of arguments are passed.

    VB Code:
    1. Sub New(arg1 As type1, arg2 As type2)
    2. ...
    3. Sub New(arg1 As type1, arg2 As type3)
    4. ...
    5. Sub New(arg1 As type1, arg2 As type2, arg3 As type3)
    6. ...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Re: [02/03] calling constructor within a constructor

    Quote Originally Posted by jmcilhinney
    Constructors are just methods so you can call them from each other like any other method, except that you would only ever call one other constructor and you would always do it as the first operation. It is preferable to have an overloaded constructor and have the more basic overloads pass default values to the more complex ones rather than have a single constructor with optional parameters.
    right, that means i can do it. i.e. calling the constructor of one class within the constructor of another class

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] calling constructor within a constructor

    Quote Originally Posted by puresmile
    right, that means i can do it. i.e. calling the constructor of one class within the constructor of another class
    No, that's not right. I didn't say anything about different classes. You can call one constructor of a type from within another constructor of that same type or a class that inherits that type. You can call Me.New, MyBase.New or MyClass.New, which each do slightly different things of course.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [02/03] calling constructor within a constructor

    In this case I would rethink about what I am doing. Having so many parameters in a constructor doesn't cound a good idea to me. You should rethink about the design. When someone uses your class and has to pass so many parameters just during instantiation then then would find it little bit tiresome.

    And as suggested you can use overloaded constructors.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Re: [02/03] calling constructor within a constructor

    Quote Originally Posted by Shuja Ali
    In this case I would rethink about what I am doing. Having so many parameters in a constructor doesn't cound a good idea to me. You should rethink about the design. When someone uses your class and has to pass so many parameters just during instantiation then then would find it little bit tiresome.

    And as suggested you can use overloaded constructors.

    problem is that i've to save all the attributes in the database. i can overload constructor.

    should i overload many constructors then call them a new constructor combining all those constructor

    is it feasible?

  10. #10
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [02/03] calling constructor within a constructor

    The better way of doing this would be to implement individual properties. And then having a save method that will save that data into the database.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width