VB.NET - 调用多个构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8667038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:08:46  来源:igfitidea点击:

VB.NET - Call multiple Constructors

.netvb.netclassconstructor

提问by manuellt

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

我是 ac# 开发人员并且没有足够的 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

在第二个 FooPresenter 构造函数中使用此代码时,我收到一个错误

"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?.

我可以创建一个方法 SetValues( ... parameters ... ) 并从两个构造函数调用它,但有人知道避免这个错误的解决方法吗?,为什么编译器不验证在重载之前调用基构造函数的可能性构造函数?

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?

有人知道如何从逻辑上证明无法同时从一个类级别构造函数调用基类构造函数和另一个类级别构造函数的事实吗?

回答by M.A. Hanin

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

问题是,一旦您指定了参数构造函数,除非您明确指定,否则无参数构造函数将变为私有

So, modifying ken2K's code:

所以,修改ken2K的代码:

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

回答by ken2k

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

就像 with 一样C#,您不能同时调用this()base()。这是你应该做的:

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

回答by Ray

You can't call multiple constructors like that. Just like in C# you can only call one initializer.

你不能像这样调用多个构造函数。就像在 C# 中一样,您只能调用一个初始化程序。