简体   繁体   中英

C# to VB.Net conversion issue

when converting the following line from C# to VB.Net I get

Expression does not produce a value

C#

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

VB.NET

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

C# code :

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);
}

I want Vb.net conversion. when i converted this code in vb.net getting error in query.ToList().ForEach(Function(ti) cat.Add(ti)) this line .error is Expression does not produce a value.

Converted VB.NET code

  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

Change the problematic line to that one:

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

That's necessary because there is a separate syntax for Action<T> ( Sub(T) ) and Func<T> ( Function(t) ) in VB.NET (but there is not in C#).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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