-3
    public bool Solve_Maze(int X_Pos, int Y_Pos)
    {
        bool Move = false;

        //Checking if the position isnt on the finish
        if (maze_board[X_Pos,Y_Pos] == 'e')
        {
            Move = true;
            maze_board[X_Pos,Y_Pos] = '+';
            return Move;
        }

        // Check for a wall
        if (maze_board[X_Pos, Y_Pos] == '1')
            return false;

        if (maze_board[X_Pos, Y_Pos] == 'X')
            return false;

        maze_board[X_Pos, Y_Pos] = 'X';

        Move = Solve_Maze(X_Pos + 1, Y_Pos);
        Move = Solve_Maze(X_Pos, Y_Pos + 1);
        Move = Solve_Maze(X_Pos - 1, Y_Pos);
        Move = Solve_Maze(X_Pos, Y_Pos - 1);

        maze_board[X_Pos, Y_Pos] = '+';

        return Move;

    }

This is the piece of code that is giving me the error

    if (maze_board[X_Pos,Y_Pos] == 'e')

I am a complete newbie when it comes to programming and was kind of thrown into the deep end with a university assignment so any help would be much appreciated

  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code (which you've done, not minimally) and accurately describe the problem. Include the entire error message -- especially the trace-back (showing specific line of failure). – Prune Dec 21 '16 at 18:25
  • 2
    See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. Among other things, start by inserting a print or two that will trace the values of **X_Pos** and **Y_Pos**. – Prune Dec 21 '16 at 18:25
  • Which line of your code throws index out of bound exception? It's difficult to go through the entire code. Be specific. – Praburaj Dec 21 '16 at 18:25
  • Because an index got out of bound. Simple. You should try to debug first. Detect which line is throwing it. – Surajeet Bharati Dec 21 '16 at 18:29
  • Ryan, you likely have declared a static member maze_board variable in your file - remove that declaration and pass the maze_board into this method as a parameter, like this: ```Solve_Maze(char[,] maze_board, int X_Pos, int Y_Pos)``` – otto-null Dec 21 '16 at 18:34
  • check your indices before accessing them.. Use some print statements to figure out for which value of `x_pos and y_pos` it throws the exception – Praburaj Dec 21 '16 at 18:38

1 Answers1

-2

In order to get that error, you must have already declared maze_board[] as a variable outside of that method. Instead, pass the maze_board in to this method as a parameter so that you are sure it is initialized and holding the current maze. This is a guess at this point because I can't see the original declaration of the maze_board variable. Can you post the entire code file?

otto-null
  • 593
  • 3
  • 15
  • The down-votes here are surprising - the OP has a variable in the method that is not declared in the provided code. In order to get an out of range error (and for the code to compile), there must be a higher-scope declaration for maze_board which is not shown here - likely as a static member of (what appears to be) a Program class for a console app. That's confusing because it's also declared in the main method. – otto-null Dec 21 '16 at 18:31
  • 1
    `The down-votes here are surprising` why? SO is not a code debugging service. Read the comments under question.... – L.B Dec 21 '16 at 18:40
  • I recognize that it's not a debugging service and so I get the down-votes on the question, but it's a learning exercise for the OP clearly which makes it a how-to as much as a why not working issue. – otto-null Dec 21 '16 at 18:44
  • `but it's a learning exercise for the OP clearly which makes it a how-to as much as a why not working issue` Link in comments explains that better https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – L.B Dec 21 '16 at 18:50
  • I agree but a down-vote is incorrectly used given that the answer is undoubtedly both correct and helpful to the OP. L.B. you can learn more about it here: http://stackoverflow.com/help/privileges/vote-down – otto-null Dec 21 '16 at 19:08
  • `undoubtedly both correct and helpful` I don't agree as you have said *This is a guess at this point* – L.B Dec 21 '16 at 19:10
  • Guesses are useful if accurate and can be quite informative even if not, and properly stated as a guess makes them clear as such. The question does not include complete information, but it is a fact that the variable must be declared out of method scope if an IoR was raised. That does not make the post "dangerous" or "sloppy" - it's specifically stated and effort was expended to help the OP with advice that is good (tell me what other explanation there can be for a compiled C# method with an undeclared variable that did compile - it must be a closure). – otto-null Dec 21 '16 at 19:15