C#不能作为参考传递

时间:2016-09-06 14:07:00

标签: c# visual-studio-2013

当我遇到这个错误时,我正在尝试使用EMGUCV进行C#编程:

错误1无法通过' currentFrameBlob'作为ref或out参数,因为它是一个foreach迭代变量'

错误2无法通过' currentFrameBlob'作为ref或out参数,因为它是一个foreach迭代变量'

它位于此代码块中。

public void matchCurrentFrameBlobsToExistingBlobs(ref List<Blob> existingBlobs, ref List<Blob> currentFrameBlobs)
{
    foreach (Blob existingBlob in existingBlobs)
    {
        existingBlob.blnCurrentMatchFoundOrNewBlob = false;
        existingBlob.predictNextPosition();
    }

    foreach (Blob currentFrameBlob in currentFrameBlobs)
    {
        int intIndexOfLeastDistance = 0;
        double dblLeastDistance = 1000000.0;
        for (int i = 0; i <= existingBlobs.Count() - 1; i++)
        {
            if ((existingBlobs[i].blnStillBeingTracked == true))
            {
                double dblDistance = distanceBetweenPoints(currentFrameBlob.centerPositions.Last(), existingBlobs[i].predictedNextPosition);

                if ((dblDistance < dblLeastDistance))
                {
                    dblLeastDistance = dblDistance;
                    intIndexOfLeastDistance = i;
                }
            }
        }

        if ((dblLeastDistance < currentFrameBlob.dblCurrentDiagonalSize * 0.5))
        {
            addBlobToExistingBlobs(ref currentFrameBlob, ref existingBlobs, ref intIndexOfLeastDistance);
        }
        else
        {
            addNewBlob(ref currentFrameBlob, ref existingBlobs);
        }
    }

我读到了错误,在我看来,这是C#的怪癖之一。处理此类错误的最有效方法是什么?现在我非常害怕做出改变,因为我很少知道如何解决它而不会让它变得更糟。

1 个答案:

答案 0 :(得分:-3)

最简单的解决方法:

 foreach (Blob currentFrameBlob2 in currentFrameBlobs)
 {
   Blob currentFrameBlob = currentFrameBlob2;
   //....
相关问题