简体   繁体   English

将 C# 翻译成 VB

[英]Translate C# to VB

I have method SwitchScreen to change userControl.我有方法 SwitchScreen 来更改 userControl。 I want change my app from C# to VB.我想将我的应用程序从 C# 更改为 VB。 I tried to find on google but no result.我试图在谷歌上找到但没有结果。 Can someone help?有人可以帮忙吗? :( :(

here is my method:这是我的方法:

 public void SwitchScreen(object sender, string getText)
    {
        var screen = (UserControl)sender;
        headerTitle.Text = getText;

        if ((screen) != null)
        {
            StackPanelMain.Children.Clear();
            StackPanelMain.Children.Add(screen);
        }
    }

i've tried like this in VB:我在VB中试过这样:

 Public Sub SwitchScreen(sender As Object, getText As String)

    Dim Screen = sender(UserControl)
    headerTitle.Text = getText

    If (Screen Is Nothing) Then
        StackPanelMain.Children.Clear()
        StackPanelMain.Children.Add(Screen)
    End If
End Sub

But error on Dim Screen = sender(UserControl), 'UserControl' is a class type and cannot be used as an expression.但是 Dim Screen = sender(UserControl), 'UserControl' 上的错误是 class 类型,不能用作表达式。

You're changing the meaning of that line when you write it that way.当您以这种方式编写时,您正在更改该行的含义。

C# C#

        var screen = (UserControl)sender;

This is taking the 'sender' var, casting as a 'UserControl' class, and setting to be referenced by the 'screen' var.这是采用“发送者”变量,转换为“用户控件”class,并设置为由“屏幕”变量引用。

VB VB

    Dim Screen = sender(UserControl)

It's been a while since I worked in VB, but it looks like you're calling a function named 'Sender' and passing 'UserControl' as a parameter, which it is not.自从我在 VB 工作以来已经有一段时间了,但看起来你正在调用一个名为“Sender”的 function 并将“UserControl”作为参数传递,但事实并非如此。

you need to find the VB equivilent of 'cast as Object' for your sender var.您需要为您的发件人 var 找到“cast as Object”的 VB 等效项。

Maybe也许

Dim Screen = CType(sender, UserControl)

This should work这应该工作

Public Sub SwitchScreen(sender As Object, getText As String)

    Dim Screen as UserControl = sender
    headerTitle.Text = getText

    If (Screen IsNot Nothing) Then
        StackPanelMain.Children.Clear()
        StackPanelMain.Children.Add(Screen)
    End If
End Sub

vb should auto cast - a cast probably not required. vb 应该自动转换 - 可能不需要转换。

Public Sub SwitchScreen(sender As Object, getText As String)

    Dim Screen As UserControl = TryCast(sender, UserControl)
    headerTitle.Text = getText

    If (Screen IsNot Nothing) Then
        StackPanelMain.Children.Clear()
        StackPanelMain.Children.Add(Screen)
    End If
End Sub

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

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