函数返回Loop里面? [英] Function return inside for Loop?

查看:65
本文介绍了函数返回Loop里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,

我有一个函数,该函数应该返回一个与函数类型相同的值,但问题是返回行嵌套在for循环中功能。



注意:我在for循环后尝试返回null,但出现错误。

如何解决这个问题?

提前致谢,

z3ngew



从非解决方案转移:

Hello Everyone,
I have a function, the function is supposed to return a value with the same type as the function, but the problem is that the return line is nested in a for loop within the function.

Note: i have tried return null after the for loop, but error appears.
How can i solve this problem?
Thanks in advance,
z3ngew

Moved from non-solution:

public CircleF Capture_Piece(Image<gray,> image)
{
	using (MemStorage storage = new MemStorage())
	{
		for (Contour<point> contours =  image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
			Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST); contours != null; contours = contours.HNext)
		{
			Contour<point> current_contour = contours.ApproxPoly(contours.Perimeter * 0.01, storage);
			if (current_contour.Area > 250)
			{
				Point[] pts = current_contour.ToArray();
				PointF[] ptsF = Array.ConvertAll(pts, new Converter<point,>(General.PointToPointF));
				CircleF circle = PointCollection.MinEnclosingCircle(ptsF);
				//return circle;
			}
		}
	}
}

推荐答案

你可以(理论上)使用返回代码中的任何一点,但有些人会抱怨你应该有一个退出,只有一个退出。我,我不同意 - 错误退出对我来说很好,就像明显的退出一样 - 它会使流量太大而不能使正常退出值得。

所以这是完全合法的要做到这一点:

You can (in theory) use a return at any point in your code, but there are those who will complain that you should have one exit, and one only. Me, I disagree - error exits are fine by me, as are "obvious" exits - where it would mess up the flow too much to make a "normal" exit worthwhile.
So it is perfectly legal to do this:
private MyClass MyMethod(List<MyClass> list)
    {
    foreach (MyClass mc in list)
        {
        if (mc.UserName == "Exit")
            {
            return mc;
            }
        }
    return null;
    }

或者这个:

Or this:

private MyClass MyMethod(List<MyClass> list)
    {
    for (int i = 0; i < list.Count; i++ )
        {
        if (list[i].UserName == "Exit")
            {
            return list[i];
            }
        }
    return null;
    }

但我会选择前者,因为我不需要知道索引或项目数。

But I would go with the former, as I have no need to know the index, or the number of items.


也许你可以简化你的代码并添加这样的回报:



Maybe you could simplify your code and add returns like this:

public CircleF Capture_Piece(Image<gray> image)
{
    using (MemStorage storage = new MemStorage())
    {
       Contour<point> contours =  image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);

       while (contours != null)
       {
           Contour<point> current_contour = contours.ApproxPoly(contours.Perimeter * 0.01, storage);
           if (current_contour.Area > 250)
           {
                Point[] pts = current_contour.ToArray();
                PointF[] ptsF = Array.ConvertAll(pts, new Converter<point,>(General.PointToPointF));
                CircleF circle = PointCollection.MinEnclosingCircle(ptsF);
                        return circle;
            }
            contours = contours.HNext;
        }
        return null;
    }
}


z3ngew写道:

return null不起作用==错误

return null is not working == error

它有三种方法。



There are three approaches to it.

  1. CircleF struct class



    几乎在所有情况下,这都非常安全,比较安全。它会立即起作用。在大多数情况下,这是最好的解决方案。
  2. 使用不同的模式:特殊的非对象值来表示null的概念。它可以是任何特殊值,例如具有零或负半径的圆。在结构中,创建一个布尔谓词方法,检查这是否为null对象,以使该条件的实现细节在结构中是私有的。您还建议您使用特殊的静态工厂方法来生成此类null对象的实例,同样,在相同结构的代码中,隐藏实现细节。



    当你拥有它时,你应该为你的回报创建这样的对象而不是null,就像现在这样做。
  3. 如果 CircleF 是一个值类型,你总是可以使用类型 CircleF?将它变成 nullable 对象。此方法主要用于原始类型和枚举类型。它也适用于 struct ,因此您可以像使用类一样使用 null 。请参阅:

    http:// msdn .microsoft.com / zh-CN / library / 1t3y8s4s%28v = vs.110%29.aspx [ ^ ]。



    然而,它没有很有意义,因为你可以更好地使用第一种方法。不过,最好还是了解它。
  1. Turn CircleF from struct to class.

    In almost all cases, this is perfectly safe, safer then the other way around. It will work immediately. In most cases, this is the best solution.
  2. Use a different pattern: special "not an object" value to express the concept of null. It could be any "special" value, such as a circle with zero or negative radius. In the structure, create a Boolean predicate method checking if this is a "null" object, to have the implementation detail of this criterion private in the structure. You are also advised to have a special static factory method to produce an instance of such "null" object, again, in the code of the same structure, to hide implementation detail.

    When you have it, you should create such object instead of "null" for your return, pretty much the way you do it now.
  3. If CircleF is a value type, you can always turn it into a nullable object using the type CircleF?. This approach is mostly used for primitive and enumeration types. It will work for struct as well, so you would be able to use null as with classes. Please see:
    http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=vs.110%29.aspx[^].

    However, it does no make much sense, as you can better use the first approach instead. Nevertheless, it''s better to know about it.





-SA


这篇关于函数返回Loop里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆