0

Please help me to convert this below code to vb.net. I have no idea to convert from anonymous method to vb.net. I'm currently using VS2010.

    public void DoWork(CustomObject obj)
    {
        var linq = (from s in storages
                     where s.Key == obj.Key
                     select s.Value).ToList();

        Action<ICustomService> act =
            delegate(ICustomService service)
            {
                service.ChangeValue(obj);
            };

        linq.ForEach(act);
    }

Thank you in advance.

Pure C# Developer

tong
  • 259
  • 5
  • 23

4 Answers4

4
Public Sub DoWork(ByVal obj As CustomObject)

    Dim values = (From s In storages
                 Where s.Key = obj.Key
                 Select s.Value).ToList()

    values.ForEach(Sub(service As ICustomService) service.ChangeValue(obj))

End Sub
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
0

Try using this online c# to vb.net code conversion:

http://www.developerfusion.com/tools/convert/csharp-to-vb/

Predator
  • 1,267
  • 3
  • 17
  • 43
  • Don't bother trying it for this question. If you feed it anonymous functions, it produces code that doesn't compile :( – MarkJ Mar 27 '11 at 20:25
-1
Public Sub DoWork(ByVal obj As CustomObject)
    Dim linq = storage.Where(Function(s) s.Key = obj.Key).[Select](Function(s) s.Value).ToList()

    Dim act As Action(Of ICustomService) = Sub(service As ICustomService)
                                               service.ChangeValue(obj)
                                           End Sub

    linq.ForEach(act)
End Sub
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • @Bala R; Thank you for yr fast reply. I also would like to ask that the code "(from s in storages where s.Key == select s.Value).ToList();" can be used in VB.NET or not? I'm just curious on it. – tong Mar 26 '11 at 17:44
  • 2
    There is no such thing as Function() Do in VB. it should just be Sub(). Also, brackets around Select aren't necessary... – Meta-Knight Mar 26 '11 at 17:45
  • 1
    Look at my answer! I didn't use a translator, my code actually works ;-) – Meta-Knight Mar 26 '11 at 17:56
  • @vcha you can from the query syntax `(from s in storage...)` but VB.Net syntax is slightly different see Meta-Knight's answer. – Bala R Mar 26 '11 at 18:11
  • @ downvoter care to give a reason? I verified that this edited snippet compiles! @Hans , perhaps? – Bala R Mar 26 '11 at 18:12
  • @Bala R; Thank you for your try anyway. – tong Mar 26 '11 at 18:48
-1

SharpDevelop is a free IDE for dotnet that supports several automatic sourcecode translations i.e. C# to VB.NET, IronRuby or IronPyton

For your example it produced

'
' * Created by SharpDevelop.
' * User: k3b
' * Date: 26.03.2011
' * Time: 18:44
' * 
' * To change this template use Tools | Options | Coding | Edit Standard Headers.
' 

Imports System

Namespace DefaultNamespace
    ''' <summary>
    ''' Description of Class1.
    ''' </summary>
    Public Class Class1
        Public Sub New()
        End Sub
        Public Sub DoWork(obj As CustomObject)
            Dim linq = (From s In storages Where s.Key = obj.Keys.Value).ToList()

            Dim act As Action(Of ICustomService) = Function(service As ICustomService) Do
                service.ChangeValue(obj)
            End Function

            linq.ForEach(act)
        End Sub
    End Class
End Namespace
k3b
  • 14,517
  • 7
  • 53
  • 85
  • @k3b; Thank you so much. I will use this site to translate the simple C# code. I think it should be ok on that but is there any reliable on-line converter? – tong Mar 26 '11 at 17:51
  • Yay for translators, but in this case the code just doesn't compile. It should be Sub instead of Function..do. You guys should at least make sure that the code compiles! – Meta-Knight Mar 26 '11 at 17:58
  • @Hans Passant: sorry i pasted only part of the generated code. Fixed my source – k3b Mar 26 '11 at 18:06
  • I posted a bugreport to the sharpdevelop-team. Sorry for wrong answer :-( – k3b Mar 26 '11 at 19:04