有没有办法打破一个返回void的函数?

时间:2012-05-29 08:41:03

标签: c# oop

我有一个函数,在给定另一个图块的情况下为特定图块设置绘制状态。绘制状态将更改的图块比较周围的图块然后相应地更新。我将尝试在下面说明

[b] [b] [a]
[b] [a] [a]
[a] [a] [a]  where a = sand && b = water

当a检测到b接近它时,它必须更新其绘制状态。所以我有一个适用于大写,小写,左大小写和右大小写的函数。我现在需要修改该功能,以便它可以处理左右情况,右上角情况,右下角情况等。这是我的功能

public override void CompareBorderingTiles(Tile T)
    {
        if (T is Water)
        {
            float leftBound = location.X - (Tile.TileWidth * Tile.TileScale);
            float rightBound = location.X + (Tile.TileWidth * Tile.TileScale);
            float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale);
            float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale);
            if (T.GridLocation.X == leftBound)
            {
                drawstate = DrawState.Left;
            }
            if (T.GridLocation.X == rightBound)
                drawstate = DrawState.Right;
            if (T.GridLocation.Y == upperBound)
                drawstate = DrawState.Upper;
            if (T.GridLocation.Y == bottomBound)
                drawstate = DrawState.Lower; 
        }

        base.CompareBorderingTiles(T);
    }

为什么我想要突破这个功能,或者可能不是,这应该是非常明确的解释。基本上我有一个枚举,告诉我我的绘制状态是什么(drawstate是枚举)。 任何人都可以告诉我是否可以设置正确的绘制状态,然后退出我的职能部门吗?

5 个答案:

答案 0 :(得分:17)

只需使用您想要结束的return语句:

return;

所以,在您的代码中,您可以这样做:

public override void CompareBorderingTiles(Tile T)
{
    if (T is Water)
    {
        float leftBound = location.X - (Tile.TileWidth * Tile.TileScale);
        float rightBound = location.X + (Tile.TileWidth * Tile.TileScale);
        float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale);
        float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale);
        if (T.GridLocation.X == leftBound)
        {
            drawstate = DrawState.Left;
            return;
        }
        if (T.GridLocation.X == rightBound)
        {
            drawstate = DrawState.Right;
            return;
        }
        if (T.GridLocation.Y == upperBound)
        {
            drawstate = DrawState.Upper;
            return;
        }
        if (T.GridLocation.Y == bottomBound)
        {
            drawstate = DrawState.Lower; 
            return;
        }
    }

    base.CompareBorderingTiles(T);
}

答案 1 :(得分:7)

单独使用return;,这将立即从函数“返回”。

虽然你真的需要回归吗?

   if (T.GridLocation.X == leftBound)
    {
        drawstate = DrawState.Left;
    }
    else if (T.GridLocation.X == rightBound)
    {
        drawstate = DrawState.Right;
    }

    else if (T.GridLocation.Y == upperBound)
    {
        drawstate = DrawState.Upper;
    }
    else if (T.GridLocation.Y == bottomBound)
    {
        drawstate = DrawState.Lower; 
    }

这应该会使代码在将来更容易维护。

答案 2 :(得分:7)

您可以使用return退出功能。

答案 3 :(得分:3)

您可以在函数的任何位置使用return;,然后它会将函数保留在那里。使用return意味着您的基本函数将被调用。如果您需要使用else if来调用基函数,那么当您满足条件时,它将不会检查剩余的if语句:

public override void CompareBorderingTiles(Tile T)
    {
        if (T is Water)
        {
            float leftBound = location.X - (Tile.TileWidth * Tile.TileScale);
            float rightBound = location.X + (Tile.TileWidth * Tile.TileScale);
            float upperBound = location.Y - (Tile.TileHieght * Tile.TileScale);
            float bottomBound = location.Y + (Tile.TileHieght * Tile.TileScale);
            if (T.GridLocation.X == leftBound)
            {
                drawstate = DrawState.Left;
            }
            else if (T.GridLocation.X == rightBound)
                drawstate = DrawState.Right;
            else if (T.GridLocation.Y == upperBound)
                drawstate = DrawState.Upper;
            else if (T.GridLocation.Y == bottomBound)
                drawstate = DrawState.Lower; 
        }

        base.CompareBorderingTiles(T);
    }

答案 4 :(得分:0)

使用return; 该函数将作为void返回