【问题标题】:C# to VB.Net conversion issueC# 到 VB.Net 的转换问题
【发布时间】:2013-07-18 06:27:16
【问题描述】:

当将以下行从 C# 转换为 VB.Net 时,我得到了

表达式不产生值

C#

query.ToList().ForEach(ti => cat.Add(ti));

VB.NET

query.ToList().ForEach(Function(ti) cat.Add(ti))

C#代码:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    new DesignerMetadata().Register();

    var toolbox = new ToolboxControl();
    var cat = new ToolboxCategory("Standard Activities");
    var assemblies = new List<Assembly>();
    assemblies.Add(typeof(Send).Assembly);
    assemblies.Add(typeof(Delay).Assembly);
    assemblies.Add(typeof(ReceiveAndSendReplyFactory).Assembly);

    var query = from asm in assemblies
                from type in asm.GetTypes()
                where type.IsPublic &&
                !type.IsNested &&
                !type.IsAbstract &&
                !type.ContainsGenericParameters &&
                (typeof(Activity).IsAssignableFrom(type) ||
                typeof(IActivityTemplateFactory).IsAssignableFrom(type))
                orderby type.Name
                select new ToolboxItemWrapper(type);

    query.ToList().ForEach(ti => cat.Add(ti));
    toolbox.Categories.Add(cat);
    Grid.SetColumn(toolbox, 0);
    Grid.SetRow(toolbox, 1);
    LayoutGrid.Children.Add(toolbox);
}

我想要 Vb.net 转换。当我在 vb.net 中转换此代码时,在 query.ToList().ForEach(Function(ti) cat.Add(ti)) 中出现错误,此行 .error 是 Expression does not generate a value。

转换后的 VB.NET 代码

  Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim metadata = New DesignerMetadata()
    metadata.Register()

    'Create the ToolBoxControl
    Dim toolbox = New ToolboxControl()

    'Create a collection of category items
    Dim cat = New ToolboxCategory("Standard Activities")
    Dim assemblies = New List(Of Assembly)()
    assemblies.Add(GetType(SendAndReceiveReplyFactory).Assembly)
    assemblies.Add(GetType(Delay).Assembly)
    assemblies.Add(GetType(ReceiveAndSendReplyFactory).Assembly)

    Dim query = _
     From asm In assemblies
     From type In asm.GetTypes() _
     Where type.IsPublic AndAlso Not type.IsNested AndAlso Not type.IsAbstract AndAlso Not type.ContainsGenericParameters AndAlso (GetType(Activity).IsAssignableFrom(type) OrElse GetType(IActivityTemplateFactory).IsAssignableFrom(type)) _
     Order By type.Name
     Select New ToolboxItemWrapper(type)

    query.ToList().ForEach(Function(ti) cat.Add(ti))
    toolbox.Categories.Add(cat)
    Grid.SetColumn(toolbox, 0)
    Grid.SetRow(toolbox, 1)
    LayoutGrid.Children.Add(toolbox)
End Sub

【问题讨论】:

  • 我编辑了您的问题,以便更清楚地说明错误在哪里,您应该尝试只在问题中包含相关信息

标签: c# vb.net visual-studio-2010 c#-to-vb.net


【解决方案1】:

将有问题的那一行改成那一行:

query.ToList().ForEach(Sub(ti) cat.Add(ti)) 

这是必要的,因为在 VB.NET 中有 Action&lt;T&gt; (Sub(T)) 和 Func&lt;T&gt; (Function(t)) 的单独语法(但在 C# 中没有)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多