简体   繁体   English

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

[英]Is there any way to break out of a function that returns void?

I have a function that sets a draw-state for a specific tile given another tile. 我有一个函数,为给定另一个tile的特定tile设置绘制状态。 The tile that's draw state is going to change compares tiles that are surrounding it and then updates accordingly. 绘制状态将更改的图块比较周围的图块然后相应地更新。 I'll try to illustrate that below 我将尝试在下面说明

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

when a detects that b is bordering it, it must update its draw-state. 当a检测到b正在接近它时,它必须更新其绘制状态。 So I have a function that works for an upper case, lower case, left case, and right case. 所以我有一个适用于大写,小写,左大小写和右大小写的函数。 I now need to modify that function so it can handle a left-right case, upper-right case, lower-right case, etc. etc. Here is my function 我现在需要修改该功能,以便它可以处理左右情况,右上角情况,右下角情况等。这是我的功能

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);
    }

It should be pretty explanatory as to why i'd want to break out of this function, or maybe not. 对于为什么我想要突破这个功能,或者可能不是这个功能,它应该是非常明确的解释。 Basically I have an enum which tells me what my draw-state is (drawstate is the enum). 基本上我有一个枚举,告诉我我的绘制状态是什么(drawstate是枚举)。 Can anybody tell me if I can set that correct draw state and then get out of my function? 谁能告诉我是否可以设置正确的绘制状态然后退出我的功能?

Just use a return statement where you want to end: 只需使用您想要结束的return语句:

return;

So, in your code you could do: 所以,在您的代码中,您可以这样做:

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);
}

Just use return; 只需使用return; on its own, this will immediately "return" from the function. 就其本身而言,这将立即从函数“返回”。

On reflection though do you really need to 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; 
    }

That should make the code easier to maintain in the future. 这应该使代码在未来更容易维护。

您可以使用return退出函数。

You could use return; 你可以使用return; at any point in the function and it will leave the function there and then. 在函数的任何一点,它将在那里离开函数。 Using return would mean your base function would not be called. 使用return意味着不会调用您的基函数。 If you require the base function to be called use else if then when you're condition is fulfilled it won't check the remaining if statements: 如果您需要基本函数被调用使用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);
    }

use return; 使用return; the function will return as a void 该函数将作为void返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SelectedIndex 在 void/static void function 中返回 -1 - SelectedIndex returns -1 in void/static void function 突破父功能? - Break out of parent function? 开关中的if内是否有足够的中断能力,足以脱离C#中的开关? - Is any break inside an if in a switch enough to break out of the switch in C#? 在void函数中返回void和不返回之间有什么区别吗? - is there any difference between return void and not return in a void function 有什么办法可以使这两个块的通用功能 - Is there any way to make common function out of these two blocks 使用 async 而不等待 function 包含另一个返回 void 的 function - Use async without await with a function that holds another function that returns void 将返回 Console.WriteLine 的 void function 转换为字符串 function? - Converting a void function that returns Console.WriteLine into a string function? 如何分解返回不同类型的方法的程序流程? - How to break out a program flow of a method that returns a different type? 有没有一种方法可以在C#中实现“ void func(out params object []参数)” - Is there a way to implement “void func(out params object[] parameters)” in C# 有什么方法可以将IntPtr / void *转换为C#中的ref SomeStruct? - Is there any way to convert IntPtr / void* to ref SomeStruct in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM