需要帮助将此c#代码更改为Vb

本文关键字:Vb 代码 帮助 | 更新日期: 2023-09-27 18:29:03

public void ChangeList(IEnumerable<LineInfo> newLineList)
{
    if (InvokeRequired)
    {
        BeginInvoke((Action<MainForm, IEnumerable<LineInfo>>)((t, e1) => t.ChangeList(e1)), this, newLineList);
        return;
    }
}

需要帮助将此c#代码更改为Vb

Public Sub ChangeList(ByVal newLineList As IEnumerable(Of LineInfo))    
    If InvokeRequired Then        
        BeginInvoke( DirectCast( (Sub(t, e1) t.ChangeList(e1)), (Action(Of MainForm, IEnumerable(Of LineInfo)) ) ), Me, newLineList)
        Exit Sub
    End If
End Sub

我在没有编译器的情况下完成了这项工作,所以BeginInvoke调用中的某个地方可能有一个放错括号的地方,但在其他方面应该是正确的。

其他人正在使用的Telerik转换器在newLineList参数中缺少泛型类型,并尝试使用Function lambda(需要返回值)而不是Sub lambda(不需要返回值。)。

根据Telerik代码转换器,它是:

Public Sub ChangeList(newLineList As IEnumerable(Of LineInfo))
    If InvokeRequired Then
        BeginInvoke(DirectCast(Function(t, e1) t.ChangeList(e1), Action(Of MainForm, IEnumerable(Of LineInfo))), Me, newLineList)
        Return
    End If    
End Sub

这不是唯一可用的转换器,这里还有另一个

编辑

你可以绕过编译器的警告,比如把它改成一个函数:

Public Function ChangeList(newLineList As IEnumerable(Of LineInfo))
    If InvokeRequired Then
        BeginInvoke(DirectCast(Function(t, e1) t.ChangeList(e1), Action(Of MainForm, IEnumerable(Of LineInfo))), Me, newLineList)        
    End If
End Function

我现在收到一个警告,它不会在所有路径上都返回值,如果您尝试使用结果,可能会得到一个null引用异常。只要你不这样做,你就应该没事。

您可以通过显式不返回任何内容来删除警告:

Public Function ChangeList(newLineList As IEnumerable(Of LineInfo))
    If InvokeRequired Then
        BeginInvoke(DirectCast(Function(t, e1) t.ChangeList(e1), Action(Of MainForm, IEnumerable(Of LineInfo))), Me, newLineList)        
    End If
    Return Nothing
End Function

这是未经测试的,但可能会帮助您

您在线尝试过c到vb转换器吗?c#到VB转换器

Public Sub ChangeList(newLineList As IEnumerable)
If InvokeRequired Then
    BeginInvoke(DirectCast(Function(t, e1) t.ChangeList(e1), Action(Of MainForm, IEnumerable(Of LineInfo))), Me, newLineList)
    Return
End If
End Sub
Public Sub ChangeList(newLineList As IEnumerable)
    If InvokeRequired Then
        BeginInvoke(DirectCast(Function(t, e1) t.ChangeList(e1), Action(Of MainForm, IEnumerable(Of LineInfo))), Me, newLineList)
        Return
    End If
End Sub

你可能想编辑你的问题,同时使用这个http://www.developerfusion.com/tools/convert/csharp-to-vb/