Search code examples
c#vb.netcomparisonemgucvcontour

Converting a specific line of a VB program into C#


Hi I'm writing a C# program based of a VB program to recognize text in images. However, I can't seem to be able to figure out the C# equivalent of this line:

listOfContoursWithData.Sort(Function(oneContourWithData, otherContourWithData) oneContourWithData.boundingRect.X.CompareTo(otherContourWithData.boundingRect.X))

This is the ContourWithData class which listOfContoursWithData is an instance of:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV.Util;

namespace TrainAndTest
{
    public class ContourWithData
    {
        const int MIN_CONTOUR_AREA = 100;

        public VectorOfPoint contour;    // contour
        public System.Drawing.Rectangle boundingRect;    // bounding rect for contour
        public double dblArea;   // area of contour

        public bool checkIfContourIsValid(){
        if ((dblArea < MIN_CONTOUR_AREA))
            return false;
        else
            return true;
    }
}

Solution

  • You can use a lambda expression:

    listOfContoursWithData.Sort((oneContourWithData, otherContourWithData) => 
                                    oneContourWithData.boundingRect.X.CompareTo(otherContourWithData.boundingRect.X));
    

    The List<ContourWithData>.Sort() method takes a Comparison<ContourWithData> as parameter. This is a delegate taking two ContourWithData instances as input and returns an int.