Constructor chaining in VB.NET?

I came across some C# code today where a constructor had a few overloads, all of which contained about 10 lines of the exact same initialization code (for private members). The only difference was that some of the constructors allowed you to pass in values for some of the properties. I refactored the code to use constructor chaining, for example:


class Foo
{    
public Foo()
{
\\ initialization code
}
public Foo(string bar):this()
{
   this.bar = bar;
}
}

After doing this I was curious how I might do this in VB.NET. I consider myself pretty good with search tools like Google, but, I couldn't find any resources that explained how to perform constructor chaining in VB.NET. Is it possible? If so, how?

8 Comments

  • Public Class Foo



    Private mBar As String

    Private mFnord As String



    Public Sub New()

    'initialisation code

    End Sub



    Public Sub New(ByVal bar As String)

    Me.New()

    mBar = bar

    End Sub



    Public Sub New(ByVal bar As String, ByVal fnord As String)

    Me.New(bar)

    mFnord = fnord

    End Sub

    End Class



    The same as calling any other function, so long as you pass a set of parameters that match a known signature, it will call the right overload.



    But the specific thing you were after was the 'Me.New()' calls.

  • Sorry for the lack of formatting...

  • I agree with Geoff. I do this a lot - sure makes it easier to overload constructors.

  • A nice feature of the way VB.Net is doing it is that you can decide when to call the other constructor. One of few things I like better with VB.Net!

  • Yeah Joakim, I agree. VB.NET's way seems more flexible than C#'s. I actually thought just calling Me.New might be the way to do it - but wasn't sure. I thought maybe there was a special syntax for it the way there is in C#.

  • 'Another approach.


    Public Class Foo

    Private mFoo As String
    Private mBar As String
    Private mSomeNumber As Integer

    Public Sub New()
    'initialisation code
    End Sub

    Public Sub New(ByVal foo As String)
    Me.New(foo, " ", 0)
    End Sub

    Public Sub New(ByVal bar As String)
    Me.New("", bar, 0)
    End Sub

    Public Sub New(Byval someNum As Integer)
    Me.New("", "", someNum)
    End Sub

    Public Sub New(ByVal foo As String, ByVal bar As String, ByVal someNum As Integer)
    'Maintain a single constructor for entire class.
    mFoo = foo
    mBar = bar
    mSomeNumber = someNum
    End Sub
    End Class

  • You can use the below method, which is not chaining, but has same effect:

    Public Class Foo
    Private mFoo As String
    Private mBar As String

    Public Sub New(Optional ByVal sFoo as String = vbNullString, Optional ByVal sBar as String = vbNullString)
    mFoo = sFoo
    mBar = sBar
    End Sub
    End Class

  • Actually, the chained constructor call in VB.NET is only valid as the first statement in the calling constructor (like C#), e.g.

    Public Sub New()
    End Sub

    Public Sub New(ByVal id As Integer)
    Me.New()
    _id = id
    End Sub



    The following is invalid:

    Public Sub New(ByVal id As Integer)
    _id = id
    Me.New()
    End Sub

Comments have been disabled for this content.