简体   繁体   English

从共享方法调用构造函数(C# 到 VB.Net)

[英]Calling constructor from shared method (C# to VB.Net)

I have not enough experience in VB.NET, I have a Windows Forms VB.NET application and I'm trying to do what in C# I'd do in this way:我在 VB.NET 方面没有足够的经验,我有一个 Windows Forms VB.NET 应用程序,我正在尝试在 C# 中做我会用这种方式做的事情:

public class MyForm{    
    MyForm(string caption) {
        _caption = caption;
        if(...){
            BaseForm.Show(caption);
      }
    }

    public static void Show(string caption) {
        new MyForm(caption);
    }
}

(BaseForm is another class I have in the application with a static method Show). (BaseForm 是我在应用程序中使用静态方法 Show 的另一个类)。 All works fine in C#, but in Visual Basic .NET I'm not able to call the constructor.在 C# 中一切正常,但在 Visual Basic .NET 中我无法调用构造函数。 The constructor has become:构造函数变成了:

Public Sub New(ByVal caption As String)
        _caption = caption
        If ...
            BaseForm.Show(caption)
        End If
End Sub

But what is the VB.NET equivalent for new classname(parameters) ?但是new classname(parameters)的 VB.NET 等价物是什么? What i've tried:我试过的:

Public Shared Sub Show(ByVal caption As String)
    MyForm.New(caption)
End Sub 

Public Shared Sub Show(ByVal caption As String)
    Me.New(caption)
End Sub

Public Shared Sub Show(ByVal caption As String)
    New MyForm(caption)
End Sub

None of these works.这些都不起作用。 It also appears that online C#-VB.NET converters give me the third option as answer (I wouldn't have tried that) but it's wrong.似乎在线 C#-VB.NET 转换器给了我第三个选项作为答案(我不会尝试),但这是错误的。 VS 2017 gives me the following errors respectively: VS 2017 分别给了我以下错误:

  1. "Constructor call is valid only as the first statement in an “构造函数调用仅作为
    instance constructor"实例构造函数”
  2. This obviously doesn't work because a shared/static method can't call an instance这显然不起作用,因为共享/静态方法不能调用实例
  3. "Syntax error" “语法错误”

EDIT: Sorry for the mistake, I originally posted a code where I was passing three arguments in the VB.NET functions.编辑:对不起,我最初发布了一个代码,我在 VB.NET 函数中传递了三个参数。 However, in my real code the functions have more arguments and I'm sure this is not the problem.然而,在我的真实代码中,函数有更多的参数,我确信这不是问题。 I thought it would have been clearer if I had only shown you the essential code since the complete code is too long but forgot to delete the additional arguments in some functions我认为如果我只向您展示基本代码会更清楚,因为完整代码太长但忘记删除某些函数中的附加参数

在 VB.NET 中,你应该使用一个临时变量:

Dim tempVar As New MyForm(caption)

The essential issue here is that VB does not allow "naked" expressions to be statements.这里的基本问题是 VB 不允许“裸”表达式是语句。 C# (and Java) restricts expressions as statements to the following: C#(和 Java)将表达式限制为以下语句:

  • Assignment expressions赋值表达式
  • Any use of ++ or --任何使用 ++ 或 --
  • Method invocations方法调用
  • Object creation expressions对象创建表达式

Regarding the first two points, VB has sort-of equivalents.关于前两点,VB 有一些等价物。 VB doesn't have a built-in assignment expression; VB 没有内置的赋值表达式; instead it has an assignment statement.相反,它有一个赋值语句。 VB doesn't have ++ or -- operators; VB 没有++--运算符; it only has += and -= assignment statements.它只有+=-=赋值语句。 In any case, via helper Functions one can make fully emulated equivalents of them in VB.在任何情况下,通过辅助函数可以在 VB 中完全模拟它们。

Of the above points, only the "Method invocations" one is allowed in VB.以上几点中,VB中只允许“方法调用”之一。 The "Object creation expressions" is not. “对象创建表达式”不是。

So something like New MyForm() is an expression, despite the pretense of Sub New() .所以像New MyForm()这样的东西是一个表达式,尽管假装是Sub New() The most straight-forward work-around is to make use of local variables (as others have already suggested):最直接的解决方法是使用局部变量(正如其他人已经建议的那样):

Dim tmp As New MyForm(Text, caption, timeout)

Another idea is using With :另一个想法是使用With

With New MyForm(Text, caption, timeout) : End With

If you figure this is the sort of thing you'll be doing a lot, maybe you could add a private or protected method to your class that does nothing:如果您认为这是您将经常做的事情,也许您可​​以向您的类添加一个不执行任何操作的私有或受保护方法:

    Private Sub NoOp()
    End Sub

and use it like so:并像这样使用它:

Call New MyForm(Text, caption, timeout).NoOp()

That works because the Call statement allows for expressions on the way towards getting at a method invocation.这是有效的,因为Call语句允许在获取方法调用的过程中使用表达式。

Alternatively, you could make a helper function in a Module like so:或者,您可以在Module创建一个辅助函数,如下所示:

#Disable Warning IDE0060 ' Remove unused parameter
    Friend Sub [Call](Of T)(x As T)
#Enable Warning IDE0060 ' Remove unused parameter
    End Sub

And then do this with it:然后用它做这个:

[Call](New MyForm(Text, caption, timeout)) ' Note square brackets on [Call]

Your MyForm constructor takes only one parameter and your Show method is wrong and should be like this to return the newly created instance.你的MyForm构造函数只接受一个参数,你的Show方法是错误的,应该像这样返回新创建的实例。

Public Shared Function Show(ByVal caption As String) As MyForm
    Return New MyForm(caption)
End Function

The real issue here is your design, you should not have any logic implemented inside of your constructor.这里真正的问题是您的设计,您不应该在构造函数内部实现任何逻辑。

Something like this instead像这样的东西

Public Sub Show()
    BaseForm.Show(_caption)
End Sub

Public Shared Sub Show(ByVal caption As String)
    Dim form = New MyForm(caption)
    form.Show()
End Sub

For these kinds of question there are many C# to VB converters, including at least 2 Free Open Source once that will do the conversion for you.对于这些类型的问题,有许多 C# 到 VB 转换器,包括至少 2 个免费开源一次可以为您进行转换。 One is mine at https://github.com/paul1956/CSharpToVB一个是我在https://github.com/paul1956/CSharpToVB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM