Click here to Skip to main content
14,356,442 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a function in C# I want to convert to vb. Never had much luck with inline functions in vb. The c# code is:

public Double CalculateMeanError(ImageBuffer target, Int32 parallelTaskCount = 4)
        {
            // checks parameters
            Guard.CheckNull(target, "target");

            // initializes the error
            Int64 totalError = 0;

            // prepares the function
            bool calculateMeanError(Pixel sourcePixel, Pixel targetPixel)
            {
                Color sourceColor = GetColorFromPixel(sourcePixel);
                Color targetColor = GetColorFromPixel(targetPixel);
                totalError += ColorModelHelper.GetColorEuclideanDistance(ColorModel.RedGreenBlue, sourceColor, targetColor);
                return false;
            }

            // performs the image scan, using a chosen method
            IList<Point> standardPath = new StandardPathProvider().GetPointPath(Width, Height);
            TransformPerPixel(target, standardPath, calculateMeanError, parallelTaskCount);

            // returns the calculates RMSD
            return Math.Sqrt(totalError/(3.0*Width*Height));
        }


What I have tried:

I tried several different code converters but it gets hung up on the inline recursive function calculateMeanError. I appreciate the help on inherit vs Implement. Here is the best vb code so far.

Public Function CalculateMeanError(ByVal target As ImageBuffer, Optional ByVal parallelTaskCount As Int32 = 4) As Double
    ' checks parameters
    Guard.CheckNull(target, "target")
    ' initializes the error
    Dim totalError As Int64 = 0
    ' prepares the function
    Dim calculateMeanError As Boolean
    Pixel
    sourcePixel
    Dim targetPixel As Pixel
    Dim sourceColor As Color = GetColorFromPixel(sourcePixel)
    Dim targetColor As Color = GetColorFromPixel(targetPixel)
    totalError = (totalError + ColorModelHelper.GetColorEuclideanDistance(ColorModel.RedGreenBlue, sourceColor, targetColor))
    Return false
    ' performs the image scan, using a chosen method
    Dim standardPath As IList(Of Point) = (New StandardPathProvider + GetPointPath(Width, Height))
    TransformPerPixel(target, standardPath, calculateMeanError, parallelTaskCount)
    ' returns the calculates RMSD
    Return Math.Sqrt((totalError / (3  _
                    * (Width * Height))))
End Function
Posted
Updated 4-Oct-19 2:35am
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try:
Public Function CalculateMeanError(ByVal target As ImageBuffer, Optional ByVal parallelTaskCount As Int32 = 4) As Double
    Guard.CheckNull(target, "target")
    
    Dim totalError As Int64 = 0
    
    Dim fn As Func(Of Pixel, Pixel, Boolean) = Function(sourcePixel As Pixel, targetPixel As Pixel)
        Dim sourceColor As Color = GetColorFromPixel(sourcePixel)
        Dim targetColor As Color = GetColorFromPixel(targetPixel)
        totalError += ColorModelHelper.GetColorEuclideanDistance(ColorModel.RedGreenBlue, sourceColor, targetColor)
        Return False
    End Function
    
    Dim standardPath As IList(Of Point) = New StandardPathProvider().GetPointPath(Width, Height)
    TransformPerPixel(target, standardPath, fn, parallelTaskCount)

    Return Math.Sqrt(totalError / (3.0 * Width * Height))
End Function
Lambda Expressions (Visual Basic) | Microsoft Docs[^]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try changing the name of the variable to something else. VB isn't case-sensitive, so the code converter might be confusing the internal var name with the function name.
   
Comments
larry118 4-Oct-19 10:42am
   
I tried both of these solutions, this is as close as I got.

Public Function CalculateMeanError(ByVal target As ImageBuffer, Optional ByVal parallelTaskCount As Int32 = 4) As Double
            ' checks parameters
            Guard.CheckNull(target, "target")
            ' initializes the error
            Dim totalError As Int64 = 0
            ' prepares the function

            Dim fn1 As Func(Of Pixel, Pixel, Boolean) = Function(ByVal sourcePixel As Pixel, ByVal targetPixel As Pixel)
                                                            Dim sourceColor As Color = GetColorFromPixel(sourcePixel)
                                                            Dim targetColor As Color = GetColorFromPixel(targetPixel)
                                                            totalError += ColorModelHelper.GetColorEuclideanDistance(ColorModel.RedGreenBlue, sourceColor, targetColor)
                                                            Return False
                                                        End Function

            Dim standardPath As IList(Of Point) = New StandardPathProvider().GetPointPath(Width, Height)
            TransformPerPixel(target, standardPath, fn1, parallelTaskCount)

            Return Math.Sqrt(totalError / (3.0 * Width * Height))
            Return False

        End Function<pre>

The error I get now is from the transformPerPixel(...,...,fn1,...) Says fn1 is value of type(Func(Of pixel,Pixel,Boolean) cannot be converted to ImageBuffer.transformPixelfunction.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100