Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET - Call multiple Constructors

I'm a c# developer and have not enought experience in VB.NET.

the scenario:

Namespace Presenters
    Public Class BaseFooPresenter

        ' assuming the public default parameterless constructor

        Public Sub New(ByVal strvar As String)
            ' TODO with strvar variabile
        End Sub

    End Class

    Public Class FooPresenter
           Inherits BaseFooPresenter

        Public Sub New(ByVal boolvar As Boolean)
            ' TODO with boolvar variabile
        End Sub

        Public Sub New(ByVal boolvar As Boolean, _
                       ByVal objvar As Object)
            MyBase.New(String.Empty)
            Me.New(true)
            ' TODO with objvar variabile
        End Sub

    End Class
End Namespace

With this code at the second FooPresenter constructor i get an error

"Constructor call is valid only at the first statement in an instance constructor."

at:

            Me.New(true)

If i invert the order i get the error at:

            MyBase.New(String.Empty)

I can create a method SetValues( ... parameters ... ) and call it from the two constructors but does someone knwos a workaround to avoid this error?, why the compiler do not validate the possibility to call the base constructor before the overloaded constructor?.

Does someone knows how to justify logically the fact that it's not possible to call the base class constructor and another class level constructor from one class level constructor at the same time?

like image 477
manuellt Avatar asked Dec 29 '11 11:12

manuellt


People also ask

How do you overload a constructor in VB net?

Constructor is a special method called 'New()' in vb.net and is defined as a Sub. Overloading feature is used most frequently to overload the constructor. We overload the constructor by defining more than one 'Sub New()' procedure.

Can you have multiple constructors for an object?

A class can have multiple constructors that assign the fields in different ways. Sometimes it's beneficial to specify every aspect of an object's data by assigning parameters to the fields, but other times it might be appropriate to define only one or a few.

How many constructors can a class have vb net?

In visual basic, a class can contain more than one constructor with a different type of arguments and the constructors will never return anything, so we don't need to use any return type, not even void while defining the constructor method in the class.

What is parameterized constructor in VB net?

In VB.NET, when we pass one or more arguments to a constructor, the constructor is known as a parameterized constructor. And the object of the class should be initialized with arguments when it is created. Let's create a program to use the parameterized constructor to pass the argument in a class.


2 Answers

The issue is that once you specify a parametric constructor, the parameterless constructor becomes private unless explicitly specified otherwise by you.

So, modifying ken2K's code:

Namespace Presenters
    Public Class BaseFooPresenter

        ' SPECIFYING the protected default parameterless constructor
        ' can also be public
        Protected Sub New()

        End Sub

        Public Sub New(ByVal strvar As String)
            ' TODO with strvar variabile
        End Sub

    End Class

    Public Class FooPresenter
        Inherits BaseFooPresenter

        Public Sub New(ByVal boolvar As Boolean)
            MyBase.New()
            ' TODO with boolvar variabile
        End Sub

        Public Sub New(ByVal boolvar As Boolean, _
                       ByVal objvar As Object)
            Me.New(boolvar)
            ' TODO with objvar variabile
        End Sub

    End Class
End Namespace
like image 130
M.A. Hanin Avatar answered Oct 21 '22 21:10

M.A. Hanin


Just like with C#, you can't call this() and base() at the same time. Here's what you should do:

Namespace Presenters
    Public Class BaseFooPresenter

        ' assuming the public default parameterless constructor

        Public Sub New(ByVal strvar As String)
            ' TODO with strvar variabile
        End Sub

    End Class

    Public Class FooPresenter
           Inherits BaseFooPresenter

        Public Sub New(ByVal boolvar As Boolean)
            MyBase.New(String.Empty)
            ' TODO with boolvar variabile
        End Sub

        Public Sub New(ByVal boolvar As Boolean, _
                       ByVal objvar As Object)
            Me.New(boolvar)
            ' TODO with objvar variabile
        End Sub

    End Class
End Namespace
like image 27
ken2k Avatar answered Oct 21 '22 23:10

ken2k