OK, here it is. It looks to me as if the variable I'm trying to return is assigned in the switch, but I'm getting the error from VS13 that "classChosen" is unassigned.

Thank you for taking a look.

        private static string ChooseClass()
        {
            string choice, classChosen;
            int classChoice;

            Console.WriteLine("Please choose a class:");
            Console.Write("(1) Fighter\n(2) Priest\n(3) Wizard");

            choice = Console.ReadLine();
            classChoice = Convert.ToInt32(choice);

            switch (classChoice)
            {
                case 1:
                    classChosen = "Fighter";
                    break;
                case 2:
                    classChosen = "Priest";
                    break;
                case 3:
                    classChosen = "Wizard";
                    break;
            }

            return classChosen;
        }

Recommended Answers

All 6 Replies

It means it could be unassigned. What if someone entered 4?

Try:

            switch (classChoice)
            {
                case 1:
                    classChosen = "Fighter";
                    break;
                case 2:
                    classChosen = "Priest";
                    break;
                case 3:
                    classChosen = "Wizard";
                    break;
                default:
                    classChosen = "Unknown";
                    break;
            }

Thank you. I hadn't built any fault tolerance in.

There is more fault tolerance to be build in.
Have a look at this snippet.

There is more fault tolerance to be build in.
Have a look at this snippet.

I hadn't built any fault tolerance in.

Not a good habit to get into. "What if this fails?" or "what if the user doesn't enter what I expect?" are key questions that should be answered very early in development. Otherwise you'll end up with brittle code. Further, adding error recovery after the fact often results in subtly broken (ie. did you miss an error case?) or terriby kludgy code (the dreaded band-aid approach).

As an example, I've been asked to refactor entire projects to include robust error handling and logging where it wasn't present before. Most of those projects needed extensive rewriting and in some cases full rearchitecting to do it satisfactorily. At least one project comes to mind where I decided to throw away all of the code and start over; it was that difficult to add what management wanted.

The problem is simple to solve. On exit from your function, the stackframe is released and the result is gone along with the release of the stack. You need persistance when the function exits. To have that, you have a static string choice, classChosen; or the input to the function has to be by reference. Change the function or declare classChosen as static.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.