views:

71

answers:

4

Is there any way to pass in an operator in VB.NET? I'm looking to reduce my lines of code and for two functions there is literally only an operator that is different.

For example, I have two functions, Darken and Lighten. I'd like to get to a single function with as little code as possible. The only difference are Greater Than and Less Than operators.

Function Darken(ByVal clr1 As Color, ByVal clr2 As Color) As Color
    Dim newR = If(clr2.R < clr1.R, clr2.R, clr1.R)
    Dim newG = If(clr2.G < clr1.G, clr2.G, clr1.G)
    Dim newB = If(clr2.B < clr1.B, clr2.B, clr1.B)
    Return Color.FromArgb(newR, newG, newB)
End Function
Function Lighten(ByVal clr1 As Color, ByVal clr2 As Color) As Color
    Dim newR = If(clr2.R > clr1.R, clr2.R, clr1.R)
    Dim newG = If(clr2.G > clr1.G, clr2.G, clr1.G)
    Dim newB = If(clr2.B > clr1.B, clr2.B, clr1.B)
    Return Color.FromArgb(newR, newG, newB)
End Function

What I'd like is something like (pseudo):

Function DarkenLighten(By Val Op As Operator, ByVal clr1 As Color, ByVal clr2 As Color) As Color
    Dim newR = If(clr2.R Op clr1.R, clr2.R, clr1.R)
    Dim newG = If(clr2.G Op clr1.G, clr2.G, clr1.G)
    Dim newB = If(clr2.B Op clr1.B, clr2.B, clr1.B)
    Return Color.FromArgb(newR, newG, newB)
End Function

Is this possible? I couldn't find any reference if it is possible.

+4  A: 

You could pass in a Func(Of Byte, Byte, Boolean) and use a lambda expression. It would still be somewhat more verbose in VB than in C#, but not too bad.

Or you could use the Func approach but have "pseudo-constants" (read-only fields) for the various delegates. Then you could just refer to GreaterThan or LessThan. I don't believe there's an easy and concise way of converting the operator itself to a delegate.

Jon Skeet
+1 Although in the specific code in the question, aren't those `If(...)` expressions just reimplementations of `Math.Min` and `Math.Max`? Why not use them instead? You could pass them as `Func(Of Byte, Byte, Boolean)`.
MarkJ
@MarkJ: Do you mean `Func(Of Byte, Byte, Byte)`?
Jon Skeet
Hmmm...okay, if this is as good as it gets, I guess it's the only way :(
Otaku
@Jon S yes, should be `Func(Of Byte, Byte, Byte)`.
MarkJ
+1  A: 

I have never seen this done. Nor is there an Operator type. At least I have never one. There is the operator keyword that is used for operator overloads but that's it.

spinon
+1  A: 

This isn't shorter, but it does combine them. It should also be faster because it doesn't use the if operator.

Function DarkenLighten(ByVal Op As String, ByVal clr1 As Color, ByVal clr2 As Color) As Color

    Dim newR As Integer
    Dim newG As Integer
    Dim newB As Integer

    If Op = ">" Then
        If clr2.R > clr1.R Then newR = clr2.R Else newR = clr1.R
        If clr2.G > clr1.G Then newG = clr2.G Else newG = clr1.G
        If clr2.B > clr1.B Then newB = clr2.B Else newR = clr1.B
    ElseIf Op = "<" Then
        If clr2.R < clr1.R Then newR = clr2.R Else newR = clr1.R
        If clr2.G < clr1.G Then newG = clr2.G Else newG = clr1.G
        If clr2.B < clr1.B Then newB = clr2.B Else newR = clr1.B
    Else
        Throw New Exception("Bad OP")
    End If

    Return Color.FromArgb(newR, newG, newB)

End Function
dbasnett
Thanks dbasnett. I had looked at this, but was looking for a way to remove mostly duplicate code.
Otaku
+2  A: 

I don't know VB.net, so I'm not sure if this is feasible, but couldn't you use something like compare()? Compare returns -1, 0, and 1 for <, == and > respectively. You could therefore pass -1, 0 or 1 to the function, as compare_value, and in your function have

if compare(a,b) == compare_value 
  do something...
Jeriko
Thanks Jeriko. VB.NET has this too, but it would have just created an `If/Else` statement.
Otaku