8

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?

Josh Siegl
  • 735
  • 1
  • 9
  • 16
  • 3
    you mean you want to stop your function? if it's true you can use `return;` for it – ahmadali shafiee May 29 '12 at 08:42
  • Does this answer your question? [How to Exit a Method without Exiting the Program?](https://stackoverflow.com/questions/3314305/how-to-exit-a-method-without-exiting-the-program) – TylerH Aug 03 '22 at 16:18

5 Answers5

19

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);
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
7

Just use 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.

Carl Winder
  • 938
  • 8
  • 18
  • Yes I believe that I should because I'm going to need to set an upper-left state, lower-left state etc based on where the tile is on my grid. – Josh Siegl May 29 '12 at 08:51
  • 1
    Yes but you only set one variable `drawstate` to any one of the 4 states. So really you don't need to return. All you want to do is do your checks set `drawstate` and the function will return by itself – Carl Winder May 29 '12 at 08:55
7

You can exit a function with return.

Joey
  • 344,408
  • 85
  • 689
  • 683
3

You could use 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. 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:

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);
    }
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • This - even if you don't want to call the `base` function, just wrap it in an else. These `else if`s are much easier to follow than lots of `return`s dotted about. – Rawling May 29 '12 at 08:47
0

use return; the function will return as a void

Umair Noor
  • 442
  • 4
  • 17