Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. [Resolved] C# to VB Adaption Problem with Lambda Expressions

[Resolved] C# to VB Adaption Problem with Lambda Expressions

Scheduled Pinned Locked Moved Visual Basic
csharplinqhtmlvisual-studiofunctional
28 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sonhospa

    Hi guys, I hope you had a nice start into 2012! I've been trying to convert this^ piece of code into VB, using VS Express 2010 and #Develop. In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that. The translated code of the test implementation reads like:

    Shared Sub Main(ByVal args() As String)
    Dim worker As New BackgroundWorker()
    worker.WorkerReportsProgress = True
    AddHandler worker.DoWork, Function(sender, e)
    ' pretend we have a collection of items to process
    Dim items(999) As Integer
    items.WithProgressReporting(Function(progress) worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work
    End Function

    AddHandler worker.ProgressChanged, Function(sender, e)
    ' make sure the figure is written to the
    ' same point on screen each time
    Console.SetCursorPosition(1, 0)
    Console.Write(e.ProgressPercentage)
    End Function
    
    worker.RunWorkerAsync()
    Console.Read()
    

    End Sub

    Unfortunately in VB the line

    items.WithProgressReporting(Function(progress) worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10)))

    throws an exception "Expression does not produce a value" at the underlined place. There's no such exception in C# where the test code compiles and executes fine. Having to implement the technique into my VB application, I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression? Thank you Mick

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    Seems to me one of your right parentheses is not at the correct position; you need one more before ForEach. :)

    Luc Pattyn [My Articles] Nil Volentibus Arduum

    S 1 Reply Last reply
    0
    • L Luc Pattyn

      Seems to me one of your right parentheses is not at the correct position; you need one more before ForEach. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      S Offline
      S Offline
      Sonhospa
      wrote on last edited by
      #3

      I changed it to

      items.WithProgressReporting((Function(progress) worker.ReportProgress(progress))).ForEach(Function(item) Thread.Sleep(10))

      as you said, but the same exception remains.

      1 Reply Last reply
      0
      • S Sonhospa

        Hi guys, I hope you had a nice start into 2012! I've been trying to convert this^ piece of code into VB, using VS Express 2010 and #Develop. In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that. The translated code of the test implementation reads like:

        Shared Sub Main(ByVal args() As String)
        Dim worker As New BackgroundWorker()
        worker.WorkerReportsProgress = True
        AddHandler worker.DoWork, Function(sender, e)
        ' pretend we have a collection of items to process
        Dim items(999) As Integer
        items.WithProgressReporting(Function(progress) worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work
        End Function

        AddHandler worker.ProgressChanged, Function(sender, e)
        ' make sure the figure is written to the
        ' same point on screen each time
        Console.SetCursorPosition(1, 0)
        Console.Write(e.ProgressPercentage)
        End Function
        
        worker.RunWorkerAsync()
        Console.Read()
        

        End Sub

        Unfortunately in VB the line

        items.WithProgressReporting(Function(progress) worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10)))

        throws an exception "Expression does not produce a value" at the underlined place. There's no such exception in C# where the test code compiles and executes fine. Having to implement the technique into my VB application, I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression? Thank you Mick

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Michael Schäuble wrote:

        I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression?

        Where is the original C# code?

        Bastard Programmer from Hell :suss:

        S 1 Reply Last reply
        0
        • L Lost User

          Michael Schäuble wrote:

          I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression?

          Where is the original C# code?

          Bastard Programmer from Hell :suss:

          S Offline
          S Offline
          Sonhospa
          wrote on last edited by
          #5

          In my initial message "this" is a link to the code (it should appear written in blue?). But it leads to the "Functional Fun" code which YOU yourself had recommended :-D in my LINQ-to-SQL question yesterday ;)

          L 1 Reply Last reply
          0
          • S Sonhospa

            In my initial message "this" is a link to the code (it should appear written in blue?). But it leads to the "Functional Fun" code which YOU yourself had recommended :-D in my LINQ-to-SQL question yesterday ;)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            ..someone having fun with your clipboard? :laugh:

            Bastard Programmer from Hell :suss:

            S 1 Reply Last reply
            0
            • L Lost User

              ..someone having fun with your clipboard? :laugh:

              Bastard Programmer from Hell :suss:

              S Offline
              S Offline
              Sonhospa
              wrote on last edited by
              #7

              I'm afraid I don't understand what you mean? Anyway, here's the link ^ again

              L 1 Reply Last reply
              0
              • S Sonhospa

                I'm afraid I don't understand what you mean? Anyway, here's the link ^ again

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                Michael Schäuble wrote:

                I'm afraid I don't understand what you mean

                My bad, I thought you were talking about another URL and that it got mixed up. Still recovering from the old year. Translators don't like lamda's, so you might take that one out before converting the code. Would result in something like below;

                AddHandler worker.ProgressChanged, AddressOf ProgressChanged
                
                worker.RunWorkerAsync()
                Console.Read()
                

                End Sub
                Public Sub ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
                ' make sure the figure is written to the
                ' same point on screen each time
                Console.SetCursorPosition(1, 0)
                Console.Write(e.ProgressPercentage)
                End Sub

                Bastard Programmer from Hell :suss:

                S 1 Reply Last reply
                0
                • L Lost User

                  Michael Schäuble wrote:

                  I'm afraid I don't understand what you mean

                  My bad, I thought you were talking about another URL and that it got mixed up. Still recovering from the old year. Translators don't like lamda's, so you might take that one out before converting the code. Would result in something like below;

                  AddHandler worker.ProgressChanged, AddressOf ProgressChanged
                  
                  worker.RunWorkerAsync()
                  Console.Read()
                  

                  End Sub
                  Public Sub ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
                  ' make sure the figure is written to the
                  ' same point on screen each time
                  Console.SetCursorPosition(1, 0)
                  Console.Write(e.ProgressPercentage)
                  End Sub

                  Bastard Programmer from Hell :suss:

                  S Offline
                  S Offline
                  Sonhospa
                  wrote on last edited by
                  #9

                  Ah, I've been trying that one for another reason: Most of my code is still in VB2008 which doesn't like multi-line lambdas anyway. The whole test procedure looks like this right now:

                  Public Sub TestProgressReporting()
                  	worker = New BackgroundWorker()
                  	worker.WorkerReportsProgress = True
                  
                  	AddHandler worker.DoWork, AddressOf DoWork
                  	AddHandler worker.ProgressChanged, AddressOf ProgressChanged
                  
                  	worker.RunWorkerAsync()
                  	Console.Read()
                  End Sub
                  
                  Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
                  	Dim items(999) As Integer
                  	items.WithProgressReporting(AddressOf ReportProgress)	' simulate some real work
                  End Sub
                  
                  Private Function ReportProgress() As Integer
                  	worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10))
                  End Function
                  
                  Private Sub ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
                  	'worker.ReportProgress()
                  	' make sure the figure is written to the
                  	' same point on screen each time
                  	Console.SetCursorPosition(1, 0)
                  	Console.Write(e.ProgressPercentage)
                  End Sub
                  

                  It still won't compile because 'progress' (underlined) is unknown.

                  L 1 Reply Last reply
                  0
                  • S Sonhospa

                    Ah, I've been trying that one for another reason: Most of my code is still in VB2008 which doesn't like multi-line lambdas anyway. The whole test procedure looks like this right now:

                    Public Sub TestProgressReporting()
                    	worker = New BackgroundWorker()
                    	worker.WorkerReportsProgress = True
                    
                    	AddHandler worker.DoWork, AddressOf DoWork
                    	AddHandler worker.ProgressChanged, AddressOf ProgressChanged
                    
                    	worker.RunWorkerAsync()
                    	Console.Read()
                    End Sub
                    
                    Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
                    	Dim items(999) As Integer
                    	items.WithProgressReporting(AddressOf ReportProgress)	' simulate some real work
                    End Sub
                    
                    Private Function ReportProgress() As Integer
                    	worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10))
                    End Function
                    
                    Private Sub ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
                    	'worker.ReportProgress()
                    	' make sure the figure is written to the
                    	' same point on screen each time
                    	Console.SetCursorPosition(1, 0)
                    	Console.Write(e.ProgressPercentage)
                    End Sub
                    

                    It still won't compile because 'progress' (underlined) is unknown.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Progress doesn't take a parameter, according to it's declaration. How did you resolve the fact that VB.NET lacks a "yield return" statement? ..and how about simply wrapping the C# code in an assembly and reference that from VB?

                    Bastard Programmer from Hell :suss:

                    S L 2 Replies Last reply
                    0
                    • L Lost User

                      Progress doesn't take a parameter, according to it's declaration. How did you resolve the fact that VB.NET lacks a "yield return" statement? ..and how about simply wrapping the C# code in an assembly and reference that from VB?

                      Bastard Programmer from Hell :suss:

                      S Offline
                      S Offline
                      Sonhospa
                      wrote on last edited by
                      #11

                      From my initial message:

                      Michael Schäuble wrote:

                      In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that.

                      According to IntelliSense 'progress' should be an integer value for 'percentProgress'. The funny thing is that it works as described as long as I stay in C# with the Main procedure - also when using the same referenced DLL for all the extensions:

                                  items
                                      .WithProgressReporting(progress => worker.ReportProgress(progress))
                                      .ForEach(item => Thread.Sleep(10)); // simulate some real work
                      

                      Line 2 results in the percentage, which is properly reported to the console. So there really must be something wrong with the lambda, which obviously is too cryptic for me... Still this is exactly the part of the code which I can't reference (main procedure "TestProgressReporting") since my application is written in VB.

                      1 Reply Last reply
                      0
                      • L Lost User

                        Progress doesn't take a parameter, according to it's declaration. How did you resolve the fact that VB.NET lacks a "yield return" statement? ..and how about simply wrapping the C# code in an assembly and reference that from VB?

                        Bastard Programmer from Hell :suss:

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #12

                        Eddy Vluggen wrote:

                        How did you resolve the fact that VB.NET lacks a "yield return" statement?

                        perhaps with patience. Yield exists since VS2010 SP1 according to this[^]. And it is simply yield, not yield return, so for once VB.NET is less verbose than C#. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        S L T 3 Replies Last reply
                        0
                        • L Luc Pattyn

                          Eddy Vluggen wrote:

                          How did you resolve the fact that VB.NET lacks a "yield return" statement?

                          perhaps with patience. Yield exists since VS2010 SP1 according to this[^]. And it is simply yield, not yield return, so for once VB.NET is less verbose than C#. :)

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          S Offline
                          S Offline
                          Sonhospa
                          wrote on last edited by
                          #13

                          Hey Luc, thanks for the hint, I'm just about downloading SP1. Still I'm afraid it wouldn't solve the problem I have: As I wrote I put all the extensions into a DLL which I'm referencing from my main VB code as well as from VB and C# test procedures. It seems pretty clear that there must be something wrong with the Lambda expressions. Would you have a look at them, please?

                          L 1 Reply Last reply
                          0
                          • S Sonhospa

                            Hey Luc, thanks for the hint, I'm just about downloading SP1. Still I'm afraid it wouldn't solve the problem I have: As I wrote I put all the extensions into a DLL which I'm referencing from my main VB code as well as from VB and C# test procedures. It seems pretty clear that there must be something wrong with the Lambda expressions. Would you have a look at them, please?

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #14

                            I seldom use lambda's, I probably can't help you. If they are in a separate DLL, why aren't you using C# for them? :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            S 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              I seldom use lambda's, I probably can't help you. If they are in a separate DLL, why aren't you using C# for them? :)

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              S Offline
                              S Offline
                              Sonhospa
                              wrote on last edited by
                              #15

                              You misunderstood me. All the extension methods, which contain the C# 'yield' operator, are in a separate DLL. My application is in VB so I have to call the extensions from VB using adapted code... and the sample code (pls. see link in the initial message) is in C# where the Lambdas work.

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Eddy Vluggen wrote:

                                How did you resolve the fact that VB.NET lacks a "yield return" statement?

                                perhaps with patience. Yield exists since VS2010 SP1 according to this[^]. And it is simply yield, not yield return, so for once VB.NET is less verbose than C#. :)

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #16

                                Cool - I never missed the statement in VB, until this question popped up :)

                                Bastard Programmer from Hell :suss:

                                1 Reply Last reply
                                0
                                • S Sonhospa

                                  Hi guys, I hope you had a nice start into 2012! I've been trying to convert this^ piece of code into VB, using VS Express 2010 and #Develop. In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that. The translated code of the test implementation reads like:

                                  Shared Sub Main(ByVal args() As String)
                                  Dim worker As New BackgroundWorker()
                                  worker.WorkerReportsProgress = True
                                  AddHandler worker.DoWork, Function(sender, e)
                                  ' pretend we have a collection of items to process
                                  Dim items(999) As Integer
                                  items.WithProgressReporting(Function(progress) worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work
                                  End Function

                                  AddHandler worker.ProgressChanged, Function(sender, e)
                                  ' make sure the figure is written to the
                                  ' same point on screen each time
                                  Console.SetCursorPosition(1, 0)
                                  Console.Write(e.ProgressPercentage)
                                  End Function
                                  
                                  worker.RunWorkerAsync()
                                  Console.Read()
                                  

                                  End Sub

                                  Unfortunately in VB the line

                                  items.WithProgressReporting(Function(progress) worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10)))

                                  throws an exception "Expression does not produce a value" at the underlined place. There's no such exception in C# where the test code compiles and executes fine. Having to implement the technique into my VB application, I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression? Thank you Mick

                                  S Offline
                                  S Offline
                                  Simon_Whale
                                  wrote on last edited by
                                  #17

                                  VB2010 has introduced sub(parameter) into the lambda specification. I haven't tested it but wouldn't the following work?

                                  items.WithProgressReporting(sub(progress) worker.ReportProgress(progress) end sub).forEach(function(item) thread.Sleep(10)))

                                  Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                                  S 1 Reply Last reply
                                  0
                                  • S Sonhospa

                                    Hi guys, I hope you had a nice start into 2012! I've been trying to convert this^ piece of code into VB, using VS Express 2010 and #Develop. In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that. The translated code of the test implementation reads like:

                                    Shared Sub Main(ByVal args() As String)
                                    Dim worker As New BackgroundWorker()
                                    worker.WorkerReportsProgress = True
                                    AddHandler worker.DoWork, Function(sender, e)
                                    ' pretend we have a collection of items to process
                                    Dim items(999) As Integer
                                    items.WithProgressReporting(Function(progress) worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work
                                    End Function

                                    AddHandler worker.ProgressChanged, Function(sender, e)
                                    ' make sure the figure is written to the
                                    ' same point on screen each time
                                    Console.SetCursorPosition(1, 0)
                                    Console.Write(e.ProgressPercentage)
                                    End Function
                                    
                                    worker.RunWorkerAsync()
                                    Console.Read()
                                    

                                    End Sub

                                    Unfortunately in VB the line

                                    items.WithProgressReporting(Function(progress) worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10)))

                                    throws an exception "Expression does not produce a value" at the underlined place. There's no such exception in C# where the test code compiles and executes fine. Having to implement the technique into my VB application, I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression? Thank you Mick

                                    U Offline
                                    U Offline
                                    User 7825588
                                    wrote on last edited by
                                    #18

                                    Try changing this line:

                                    items.WithProgressReporting(Function(progress)
                                    worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work

                                    To this:

                                    items.WithProgressReporting(Function(progress) _
                                    worker.ReportProgress(progress)).ToList.ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work

                                    The .ForEach extension method doesn't work with IEnumerables and since that's what WithProgressReporting returns, the resultset must first be cast as a Generic List. Also, I don't know for sure, but it looks like the converter inserted an extra line break. That's why I added the underscore to the end of the first line.

                                    S 1 Reply Last reply
                                    0
                                    • S Sonhospa

                                      Hi guys, I hope you had a nice start into 2012! I've been trying to convert this^ piece of code into VB, using VS Express 2010 and #Develop. In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that. The translated code of the test implementation reads like:

                                      Shared Sub Main(ByVal args() As String)
                                      Dim worker As New BackgroundWorker()
                                      worker.WorkerReportsProgress = True
                                      AddHandler worker.DoWork, Function(sender, e)
                                      ' pretend we have a collection of items to process
                                      Dim items(999) As Integer
                                      items.WithProgressReporting(Function(progress) worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work
                                      End Function

                                      AddHandler worker.ProgressChanged, Function(sender, e)
                                      ' make sure the figure is written to the
                                      ' same point on screen each time
                                      Console.SetCursorPosition(1, 0)
                                      Console.Write(e.ProgressPercentage)
                                      End Function
                                      
                                      worker.RunWorkerAsync()
                                      Console.Read()
                                      

                                      End Sub

                                      Unfortunately in VB the line

                                      items.WithProgressReporting(Function(progress) worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10)))

                                      throws an exception "Expression does not produce a value" at the underlined place. There's no such exception in C# where the test code compiles and executes fine. Having to implement the technique into my VB application, I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression? Thank you Mick

                                      E Offline
                                      E Offline
                                      Estys
                                      wrote on last edited by
                                      #19

                                      I always have trouble with anonymous delegates in VB so I factored them out :

                                      Class Program
                                      Shared worker As BackgroundWorker
                                      Public Shared Sub Main() 'ByVal args As String())
                                      worker = New BackgroundWorker()
                                      worker.WorkerReportsProgress = True
                                      AddHandler worker.DoWork, AddressOf DoWork
                                      AddHandler worker.ProgressChanged, AddressOf ProgressChanged
                                      worker.RunWorkerAsync()
                                      Console.Read()
                                      End Sub

                                      Private Shared Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
                                          Dim items = Enumerable.Range(1, 1000)
                                          items.WithProgressReporting(AddressOf ReportProgress).ForEach(AddressOf DoSleep)
                                      End Sub
                                      Private Shared Sub ReportProgress(ByVal progress As Integer)
                                          worker.ReportProgress(progress)
                                      End Sub
                                      Private Shared Sub DoSleep(ByVal item As Integer)
                                          Thread.Sleep(10)
                                      End Sub
                                      Private Shared Sub ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
                                          Console.SetCursorPosition(1, 0)
                                          Console.Write(e.ProgressPercentage)
                                      End Sub
                                      

                                      End Class

                                      Cheers

                                      S 1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        Eddy Vluggen wrote:

                                        How did you resolve the fact that VB.NET lacks a "yield return" statement?

                                        perhaps with patience. Yield exists since VS2010 SP1 according to this[^]. And it is simply yield, not yield return, so for once VB.NET is less verbose than C#. :)

                                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                                        T Offline
                                        T Offline
                                        Terence Wallace
                                        wrote on last edited by
                                        #20

                                        Hey just wanted to say thanks Luc that link got me to several other links on the subject and could prove quite useful to me in the very near future. Thanks again. This is now supported in VS 2010 SP1, with the Async CTP, see: http://msdn.microsoft.com/en-us/vstudio/gg497937[^] Also see: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=4738205d-5682-47bf-b62e-641f6441735b&displaylang=en[^]

                                        "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair

                                        1 Reply Last reply
                                        0
                                        • S Sonhospa

                                          Hi guys, I hope you had a nice start into 2012! I've been trying to convert this^ piece of code into VB, using VS Express 2010 and #Develop. In order to avoid problems with conversion of c# 'yield' operator, I put the extensions into a DLL and set a reference to that. The translated code of the test implementation reads like:

                                          Shared Sub Main(ByVal args() As String)
                                          Dim worker As New BackgroundWorker()
                                          worker.WorkerReportsProgress = True
                                          AddHandler worker.DoWork, Function(sender, e)
                                          ' pretend we have a collection of items to process
                                          Dim items(999) As Integer
                                          items.WithProgressReporting(Function(progress) worker.ReportProgress(progress)).ForEach(Function(item) Thread.Sleep(10)) ' simulate some real work
                                          End Function

                                          AddHandler worker.ProgressChanged, Function(sender, e)
                                          ' make sure the figure is written to the
                                          ' same point on screen each time
                                          Console.SetCursorPosition(1, 0)
                                          Console.Write(e.ProgressPercentage)
                                          End Function
                                          
                                          worker.RunWorkerAsync()
                                          Console.Read()
                                          

                                          End Sub

                                          Unfortunately in VB the line

                                          items.WithProgressReporting(Function(progress) worker.ReportProgress(progress).ForEach(Function(item) Thread.Sleep(10)))

                                          throws an exception "Expression does not produce a value" at the underlined place. There's no such exception in C# where the test code compiles and executes fine. Having to implement the technique into my VB application, I'd like to understand where the problem arises. Could anyone of you tell me what's wrong in the (automatic) translation of the Lambda expression? Thank you Mick

                                          D Offline
                                          D Offline
                                          DavidSherwood
                                          wrote on last edited by
                                          #21

                                          In VB, a function that returns void is a "Sub". This is true with lambda function as well. So your ForEach lambda should be

                                          ForEach(Sub(item) Thread.Sleep(10))

                                          S S 2 Replies Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups