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. LINQ
  4. How to convert this from VB.Net to C#? (lambdas)

How to convert this from VB.Net to C#? (lambdas)

Scheduled Pinned Locked Moved LINQ
csharptutorialquestion
5 Posts 3 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.
  • F Offline
    F Offline
    Furty
    wrote on last edited by
    #1

    This compiles in VB.Net:

    Protected Shared Function CombineFunc(Of T)(ByVal d1 As Func(Of T, Boolean), _
    ByVal condType As Compare, _
    ByVal d2 As Func(Of T, Boolean)) As Func(Of T, Boolean)

            'Return a delegate which combines delegates d1 and d2
            Select Case condType
                Case Compare.Or : Return Function(x) d1(x) Or d2(x)
                Case Compare.And : Return Function(x) d1(x) And d2(x)
                Case Compare.Xor : Return Function(x) d1(x) Xor d2(x)
                Case Compare.Equal : Return Function(x) d1(x) = d2(x)
                Case Compare.OrElse : Return Function(x) d1(x) OrElse d2(x)
                Case Compare.AndAlso : Return Function(x) d1(x) AndAlso d2(x)
                Case Compare.NotEqual : Return Function(x) d1(x) <> d2(x)
                Case Compare.LessThan : Return Function(x) d1(x) < d2(x)
                Case Compare.GreaterThan : Return Function(x) d1(x) > d2(x)
                Case Compare.LessThanOrEqual : Return Function(x) d1(x) <= d2(x)
                Case Compare.GreaterThanOrEqual : Return Function(x) d1(x) >= d2(x)
                Case Else
                    Throw New ArgumentException("Not a valid Condition Type", "condType")
            End Select
        End Function
    

    But my conversion to C# does not:

    protected static Func<T, bool> CombineFunc<T>(Func<T, bool> d1, Compare condType, Func<T, bool> d2)
    {
    //Return a delegate which combines delegates d1 and d2
    switch (condType)
    {

                    case Compare.Or:
                        return (x) => d1(x) | d2(x);
                    case Compare.And:
                        return (x) => d1(x) & d2(x);
                    case Compare.Xor:
                        return (x) => d1(x) ^ d2(x);
                    case Compare.Equal:
                        return (x) => d1(x) == d2(x);
                    case Compare.OrElse:
                        return (x) => d1(x) || d2(x);
                    case Compare.AndAlso:
                        return (x) => d1(x) && d2(x);
                    case Compare.NotEqual:
                        return (x) => d1(x) != d2(x);
                    case Compare.LessThan:
                        return (x) => d1(x) < d
    
    J 1 Reply Last reply
    0
    • F Furty

      This compiles in VB.Net:

      Protected Shared Function CombineFunc(Of T)(ByVal d1 As Func(Of T, Boolean), _
      ByVal condType As Compare, _
      ByVal d2 As Func(Of T, Boolean)) As Func(Of T, Boolean)

              'Return a delegate which combines delegates d1 and d2
              Select Case condType
                  Case Compare.Or : Return Function(x) d1(x) Or d2(x)
                  Case Compare.And : Return Function(x) d1(x) And d2(x)
                  Case Compare.Xor : Return Function(x) d1(x) Xor d2(x)
                  Case Compare.Equal : Return Function(x) d1(x) = d2(x)
                  Case Compare.OrElse : Return Function(x) d1(x) OrElse d2(x)
                  Case Compare.AndAlso : Return Function(x) d1(x) AndAlso d2(x)
                  Case Compare.NotEqual : Return Function(x) d1(x) <> d2(x)
                  Case Compare.LessThan : Return Function(x) d1(x) < d2(x)
                  Case Compare.GreaterThan : Return Function(x) d1(x) > d2(x)
                  Case Compare.LessThanOrEqual : Return Function(x) d1(x) <= d2(x)
                  Case Compare.GreaterThanOrEqual : Return Function(x) d1(x) >= d2(x)
                  Case Else
                      Throw New ArgumentException("Not a valid Condition Type", "condType")
              End Select
          End Function
      

      But my conversion to C# does not:

      protected static Func<T, bool> CombineFunc<T>(Func<T, bool> d1, Compare condType, Func<T, bool> d2)
      {
      //Return a delegate which combines delegates d1 and d2
      switch (condType)
      {

                      case Compare.Or:
                          return (x) => d1(x) | d2(x);
                      case Compare.And:
                          return (x) => d1(x) & d2(x);
                      case Compare.Xor:
                          return (x) => d1(x) ^ d2(x);
                      case Compare.Equal:
                          return (x) => d1(x) == d2(x);
                      case Compare.OrElse:
                          return (x) => d1(x) || d2(x);
                      case Compare.AndAlso:
                          return (x) => d1(x) && d2(x);
                      case Compare.NotEqual:
                          return (x) => d1(x) != d2(x);
                      case Compare.LessThan:
                          return (x) => d1(x) < d
      
      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Wow. Another VB abomination. If I were to guess at what this is doing, I'd guess that it's doing a dynamic comparison -- e.g. at runtime, it would see if the return type (always bool?) can be compared? Hmm. An alternative theory is that it should be Func<bool,T>, not Func<T, bool>. That is, it returns T, which might work for such comparisons at runtime. Compiling your code there and looking it up in reflector doesn't help much. Looks like the VB compiler creates some lambda classes under the hood and uses those for comparison. Can't quite figure out what it's doing exactly.

      Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      F 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Wow. Another VB abomination. If I were to guess at what this is doing, I'd guess that it's doing a dynamic comparison -- e.g. at runtime, it would see if the return type (always bool?) can be compared? Hmm. An alternative theory is that it should be Func<bool,T>, not Func<T, bool>. That is, it returns T, which might work for such comparisons at runtime. Compiling your code there and looking it up in reflector doesn't help much. Looks like the VB compiler creates some lambda classes under the hood and uses those for comparison. Can't quite figure out what it's doing exactly.

        Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        F Offline
        F Offline
        Furty
        wrote on last edited by
        #3

        Judah Himango wrote:

        An alternative theory is that it should be Func<bool,t>, not Func<t,>. That is, it returns T, which might work for such comparisons at runtime.

        Only some of course, so that still wouldn't explain it :~ Still lost on this one. What I've done for now is ditch the method you see here all togeher and the "Compare" enum associated with it, and have replaced it with code that makes sense (well, to a C# guy) and compiles. I'd still love to find out whether the above example is just me missing the point on the conversion to c#, or whether it's an anomoly that allows really, really shaky VB code to compile.

        J 1 Reply Last reply
        0
        • F Furty

          Judah Himango wrote:

          An alternative theory is that it should be Func<bool,t>, not Func<t,>. That is, it returns T, which might work for such comparisons at runtime.

          Only some of course, so that still wouldn't explain it :~ Still lost on this one. What I've done for now is ditch the method you see here all togeher and the "Compare" enum associated with it, and have replaced it with code that makes sense (well, to a C# guy) and compiles. I'd still love to find out whether the above example is just me missing the point on the conversion to c#, or whether it's an anomoly that allows really, really shaky VB code to compile.

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Furty wrote:

          Only some of course, so that still wouldn't explain it

          Right, and maybe the VB compiler is doing run-time resolution of that type ala duck typing. These are all just guesses, though, as I'm a pure-blooded C# man myself. :-D

          Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          G 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Furty wrote:

            Only some of course, so that still wouldn't explain it

            Right, and maybe the VB compiler is doing run-time resolution of that type ala duck typing. These are all just guesses, though, as I'm a pure-blooded C# man myself. :-D

            Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            I wouldn't call it run-time resolution, but it seems that the actual data used for True and False are just integers 1 and 0, and the code compiles to an integer comparison. This will end up giving the same result as calling Boolean.CompareTo(Boolean). Seeing as none of the integral data types seem to have operators defined that I can see, I don't see any reason why the C# compiler should prevent you from using those expressions.

            1 Reply 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