|
-
Jul 4th, 2006, 11:40 PM
#1
Thread Starter
Addicted Member
[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:
Sub New (ByVal id As Integer, ByVal name As string, ByVal charges As itemCharges)
End Sub
or any other way to do it?
-
Jul 4th, 2006, 11:45 PM
#2
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?
-
Jul 4th, 2006, 11:55 PM
#3
Thread Starter
Addicted Member
Re: [02/03] calling constructor within a constructor
 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
-
Jul 5th, 2006, 12:02 AM
#4
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.
-
Jul 5th, 2006, 12:03 AM
#5
Re: [02/03] calling constructor within a constructor
Address, City, Country - you could put them in a "Location" struct.
VB Code:
Public Type LocationStruct
Address As String
City As String
Country As String
End Type
And you can then pass that to the constructor:
VB Code:
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:
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:
Sub New(arg1 As type1, arg2 As type2)
...
Sub New(arg1 As type1, arg2 As type3)
...
Sub New(arg1 As type1, arg2 As type2, arg3 As type3)
...
-
Jul 5th, 2006, 12:10 AM
#6
Thread Starter
Addicted Member
Re: [02/03] calling constructor within a constructor
 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
-
Jul 5th, 2006, 12:19 AM
#7
Re: [02/03] calling constructor within a constructor
 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.
-
Jul 5th, 2006, 12:32 AM
#8
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
-
Jul 5th, 2006, 03:40 AM
#9
Thread Starter
Addicted Member
Re: [02/03] calling constructor within a constructor
 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?
-
Jul 5th, 2006, 03:46 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|