Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

oop

I have a function that sets a draw-state for a specific tile given another 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. 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). Can anybody tell me if I can set that correct draw state and then get out of my function?

like image 508
Josh Siegl Avatar asked May 29 '12 08:05

Josh Siegl


People also ask

How do you break out of a void function?

Use the return keyword to exit from a method. public void someMethod() { //... a bunch of code ... if (someCondition()) { return; } //... otherwise do the following... }

Can you break out of a function?

Using return is the easiest way to exit a function. You can use return by itself or even return a value.


1 Answers

Just use a return statement where you want to end:

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);
}
like image 162
Oded Avatar answered Sep 23 '22 14:09

Oded