overloaded constructors

  • 14 years ago

    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









































  • 14 years ago

    This is the thing about book examples...  They don't always make sense.  You're right for being confused, why would you leave off minutes and or seconds?  It doesn't really make sense.

    Here is the real reason for Overloading a Constructor (their example is more like Default/Optional Parameters).  (I wrote this code in C# and converted it with this site's converter so it might not work correctly but the idea is the same). 

    Imports System
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Namespace CoLithium.ImageManipulation

    Public Class InvalidPixelFormatException
    Inherits System.ApplicationException
    Const SupportedFormats As String = "Supported formats are: 16bppRgb555, 16bppRgb565, 24bppRgb, 32bppRgb, 32bppArgb, and 32bppPArgb."

    Public Sub New(ByVal bmp As Bitmap)
    MyBase.New(bmp.PixelFormat.ToString + " is an unsupported PixelFormat. " + SupportedFormats)
    End Sub

    Public Sub New(ByVal format As PixelFormat)
    MyBase.New(format.ToString + " is an unsupported PixelFormat. " + SupportedFormats)
    End Sub

    Public Sub New()
    MyBase.New("Invalid PixelFormat. " + SupportedFormats)
    End Sub
    End Class
    End Namespace






















    You Overload a Constructor to make the life of the person using you Class easier.  I wrote my Exception to work whether the user's code had a Bitmap that was in the wrong PixelFormat, just had a wrong PixelFormat not associated with an actual Bitmap, or just needed to throw the Exception without either.

    Besides this example, you may also want to write Constructors that initialize some of your Class's Properties, just as a convenience.








Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”