Hello. I'm reading about overloaded constructors. And while reading, I had a question that the book (Deitel VB.NET How to Program, p310) hasnt yet addressed.
Here is a section from the example code, which is creating a class named CTime2
Public Sub New ()
SetTime ()
End Sub
Public Sub New (ByVal hourValue As Integer) 'hour supplied to constructor
SetTime(hourValue)
End Sub
Public Sub New (ByVal hourValue As Integer, ByVal minuteValue As Integer)
SetTime (hourValue, minuteValue) 'hour and minute supplied
End Sub
Public Sub New (ByVal hourValue As Integer, ByVal minuteValue As Integer, ByVal secondValue As Integer) 'hour, minute and second supplied
SetTime (hourValue, minuteValue, secondValue)
End Sub
________________________
then, some objects are created that invoke these various constructors. my question is, why do we need to write the above seperate, 3 constructors ? why cant we just create 1 constructor, like this
Public Sub New (ByVal hourValue As Integer, ByVal minuteValue As
Integer, ByVal secondValue As Integer) 'hour, minute and second supplied
SetTime (hourValue, minuteValue, secondValue)
End Sub
which is the final constructor anyway ? Why do we have to have 3 seperate constructors or am I missing something important ?
I understand how to invoke these constructors and pass values to them, Im just a bit foggy about the (to me) long road taken in creating them in the first place
Dim time1 As New CTime(2)
Dim time2 As New CTime (21,34)
Dim time3 As New CTime (12, 13, 43)
Thanks.
DA
Enter your message below
Sign in or Join us (it's free).