0 Replies - 9354 Views - Last Post: 27 August 2012 - 04:46 AM

Poll: Do You Use Class Constructors And How (2 member(s) have cast votes)

Do You Use Class Constructors And How

  1. What is the constructor? (0 votes [0.00%])

    Percentage of vote: 0.00%

  2. I usually use ONLY ONE constructor in my classes (0 votes [0.00%])

    Percentage of vote: 0.00%

  3. I usually use constructors WITH OPTIONAL parameters (0 votes [0.00%])

    Percentage of vote: 0.00%

  4. I usually use constructor OVERLOADING (2 votes [66.67%] - View)

    Percentage of vote: 66.67%

  5. I usually use NESTED INVOCATIONS (0 votes [0.00%])

    Percentage of vote: 0.00%

  6. I usually use PRIVATE CONSTRUCTORS (0 votes [0.00%])

    Percentage of vote: 0.00%

  7. I use OBJECT INITIALIZERS (1 votes [33.33%] - View)

    Percentage of vote: 33.33%

Vote Guests cannot vote

#1 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Class Constructors Best Practices (Overloading, Optional parameters..)

Posted 27 August 2012 - 04:46 AM

I'm interested in your practices of using constructors. What is your opinion on using optional parameters for constructors versus constructor overloading. For example:
    Public Class SampleClass

    Private myName As String
    Private myAge As Integer = 99

    'constructor with optional parameter
    Public Sub New(ByVal name As String, Optional ByVal age As Integer = 0)
        myName = name

        If age <> 0 Then
            myAge = age
        End If
    End Sub

    End Class


    Public Class SampleClass
    'the same result with constructor overloading example

    Private myName As String
    Private myAge As Integer = 99

    Public Sub New(ByVal name As String)
        myName = name
    End Sub

    Public Sub New(ByVal name As String, ByVal age As Integer)
        myName = name
        myAge = age
    End Sub
    End Class



What about neseted invocations?

    'Nested constructors example
    Public Class SampleClass
    Private myName As String
    Private myAge As Integer

    Public Sub New(ByVal name As String)
        Me.New(name, 99)
    End Sub

    Public Sub New(ByVal name As String, ByVal age As Integer)
        myName = name
        myAge = age
    End Sub
    End Class



Is it just a matter of coders flawor, or does it depend more on situation (if yes, where is first example of better use than the second)?

The last thing I'd like some opinion are private constructors. What are your thoughts, comparing those lines with previous two examples:
    'Private constructors example
    Public Class SampleClass
    Private myName As String
    Private myAge As Integer

    Private Sub New()
        myName = "Default Name"
        myAge = 99
    End Sub

    Public Sub New(ByVal name As String)
        Me.New()
        Me.myName = name
    End Sub
    Public Sub New(ByVal age As Integer)
        Me.New()
        Me.myAge = age
    End Sub
    End Class



In addition, what about the usage of object initializers?

    'Object initializer example
    Public Class SampleClass
    Public Name As String
    Public Age As Integer

    '... other class stuff
    End Class


    Private Sub InitializeClassInstance()
        Dim myClassInstance As New SampleClass With {.Name = "lucky3", . Age = 696 ' ;-)
    End Sub



In general, I'd like to hear opinions for when one usage is more sutable than the other, or are those just different ways to the same goal, and are really just coding style dependant.

This post has been edited by lucky3: 27 August 2012 - 04:49 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1