Sign in to follow this  
sevak

C# variable problem

Recommended Posts

sevak    100
I have this code but it wont compile because i get this compile time error. Error 1 Use of unassigned local variable 'iChoice' However, I have assigned a value to iChoice in the do loop. Can someone help me out.
static int iMenu()
        {
            bool bInvalidNumber = false;
            int iChoice;
            do
            {
                Console.WriteLine("1.Easy\n2.Normal\n3.Hard\n4.Exit");
                try
                {
                    iChoice = Int32.Parse(Console.ReadLine());
                    if (iChoice >= 1 && iChoice <= 4)
                        break;
                    else
                        throw new Exception();
                }
                catch
                {
                    Console.WriteLine("Invalid choice, please try again.");
                    bInvalidNumber = true;
                }
            } while (bInvalidNumber);
            return iChoice;
        }



Share this post


Link to post
Share on other sites
Washu    7829
Look carefully. If Int32.Parse throws then iChoice is never assigned a value. While the do loop will generally take care of this problem, just assign iChoice a default value. Of course, a better option is to just return from within the do loop.

Share this post


Link to post
Share on other sites
yaroslavd    150
My guess is that since it's in a try-catch block, the compiler thinks that the exception might be thrown before the value is assigned. Thus, the assignment is not guaranteed. I'd just assign 0 to iChoice before the loop.

Share this post


Link to post
Share on other sites
sevak    100
I tried assigning a value to iChoice in the catch statement and it worked but I still don’t understand this because even if the parse method throws, the program will just loop again until a value for Ichoice is assigned. So there is no way for a value to not be assigned to iChoice unless the program is closed and in that case it wouldn’t matter.

edit: Thanks Washu and Yaroslvad. I posted this before seeing Washu's edit and yaroslavad's post.

Share this post


Link to post
Share on other sites
MButchers    123
Hi there,

this seems fair enough, i would tend to do this in a different way

#1 use Convert.ToInt32(Console.ReadLine());
#2 as you have the return outside the do..while the compiler will detect that you are returning a variable that has not been assigned , as you cannot exit the loop unless the variable is in 1..4 you would be better off returning -1 as a constant return at the end of the proc and using return iChoice instead of the break; keyword after the type conversion ( as washu says ).

Hope this helps

Mark

Share this post


Link to post
Share on other sites
Washu    7829

static int iMenu()
{
bool bInvalidNumber = false;
do
{
Console.WriteLine("1.Easy\n2.Normal\n3.Hard\n4.Exit");
try
{
int iChoice = Int32.Parse(Console.ReadLine());
if (iChoice >= 1 && iChoice <= 4)
return iChoice;
else
throw new Exception();
}
catch
{
Console.WriteLine("Invalid choice, please try again.");
bInvalidNumber = true;
}
} while (bInvalidNumber);
return iChoice;
}

Share this post


Link to post
Share on other sites
MButchers    123
Washu,

that wont work as your returning iChoice which is declared
in the scope of the do..while, the iChoice will need to be declared
outside of this

eg

int iChoice = -1;

do
{
...
} while (iChoice < 0);

return iChoice;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this