Ask a questionAsk a question
 

AnswerClarification needed

  • Friday, November 06, 2009 7:25 PMmagmo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi

    I need some explanation of the following code...

        Class Car
            Public id As Integer
            Public manufacturer As String
            Public color As String
        End Class

        Function GetCars() As List(Of Car)

            Dim ret = New List(Of Car)
            ret.Add(New Car With {.id = 1, .manufacturer = "Toyota", .color = "Red"})
            ret.Add(New Car With {.id = 2, .manufacturer = "Honda", .color = "Red"})

            Return ret
        End Function


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim cars = GetCars()
            Dim dictionary = cars.ToDictionary(Function(c) c.id)
        End Sub


    Where does the "c" in ToDictionary(Function(c) come from, and what is that?

Answers

  • Friday, November 06, 2009 7:34 PMd13mr3m1x Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Its the vb way of doing lambda expressions

    In c#:

    var dictionary = cars.ToDictionary(c => c.id);

    the same as Dim dictionary = cars.ToDictionary(Function(c) c.id)

    Don't know all the high tech jizz maybe one of the mods can chime in but I'm pretty sure thats what it means.

    http://msdn.microsoft.com/en-us/library/bb531253.aspx

    Thats a good microsoft link into vb.net lambdas.

    Hope this helps!

    P.s. you can define the c in anyway you want doesnt have to be a c e.g. Dim dictionary = cars.ToDictionary(Function(testForLambda) testForLambda.id)

All Replies

  • Friday, November 06, 2009 7:34 PMd13mr3m1x Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Its the vb way of doing lambda expressions

    In c#:

    var dictionary = cars.ToDictionary(c => c.id);

    the same as Dim dictionary = cars.ToDictionary(Function(c) c.id)

    Don't know all the high tech jizz maybe one of the mods can chime in but I'm pretty sure thats what it means.

    http://msdn.microsoft.com/en-us/library/bb531253.aspx

    Thats a good microsoft link into vb.net lambdas.

    Hope this helps!

    P.s. you can define the c in anyway you want doesnt have to be a c e.g. Dim dictionary = cars.ToDictionary(Function(testForLambda) testForLambda.id)
  • Friday, November 13, 2009 2:45 AMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi magmo,

     

    How is the problem now?   I think d13mr3m1x has provided a really helpful reply. 

     

    If you need any further assistance, please feel free to let me know.

     

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.