Used of Unassigned local variable

dn54321

New member
Joined
Dec 29, 2014
Messages
1
Programming Experience
Beginner
Soo i ran my code to find out LINE 30 and LINE 42 has an issue. Used of Unassigned local variable 'c' 'd'
if you can help me solve it, it would be great! Be sure to provide me with major feedback about the problems i've done and how i can improve :) i'm still very terrible at c# this is my first program so don't throw code at me without an explaination of what it does.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CPractise
{
    class Startup
    {
        static void Main(string[] args)
        {
            int x = 0;
            while (x != -1)
            {
                Console.WriteLine("What would you like? Type help for all commands!");
                string ans = Console.ReadLine();
                if (ans == "help")
                {
                    Console.WriteLine("Methods - Displays all methods used in this program");
  
                }
                else if (ans == "Methods")
                {
                    Console.WriteLine("Power - Allows program to calculate power...");
                }
                else if (ans == "Power")
                {
                    int a; int b; string c; string d;
                    Console.WriteLine("Welcome to Power Method! Type exit to exit \n Please insert the base number:");
                    while (!(c = "exit"))                                                                                                                                  //line 30
                    {
                        c = Console.ReadLine();
                        if (c == "exit")
                        {
                            Console.WriteLine("Exiting...");
                            System.Threading.Thread.Sleep(1000);
                            Console.Clear();
                        }
                        else if (int.TryParse(c, out a))
                        {
                            a = int.Parse(c);
                            while (!(d == "exit") || int.TryParse(d, out b)) //line 42
                            {
                                Console.WriteLine("Now insert power:");
                                d = Console.ReadLine();
                                if (d == "exit")
                                {
                                    Console.WriteLine("Exiting...");
                                    System.Threading.Thread.Sleep(1000);
                                    Console.Clear();
                                }
                                else if (int.TryParse(d, out b))
                                {
                                    b = int.Parse(d);
                                    Power ans1 = new Power();
                                    int answer = ans1.p(a, b);
                                    Console.WriteLine("Base Number = {0} \n Power Number = {1} \n Result of {0}^{1} = {2}", a, b, ans1);
                                }
                                else Console.WriteLine("Silly user... {0} is not a number..", d);
                            }
                        }
                        else Console.WriteLine("Silly user... {0} is not a number..", c);
                    }
                }
                else
                {
                    Console.WriteLine("Error 101: Command is invalid \n Type help for further information...");
                    System.Threading.Thread.Sleep(3000);
                    Console.Clear();
                }
            }
        }
    }
}


    }
}
People are actually bothered to read this..
 
Last edited:
Line 30:
Operator '!' cannot be applied to operand of type 'string'

The equal sign indicates that you are assigning a value to variable 'c', not a check if they are equal. Compare what with you e.g. do a few lines further in if (c == "exit").

Line 42:
You're using the variable 'd' but have not assigned a value to it yet (actually the message is self-explaining). To get rid of this message, assign a value, e.g. when you declare the variable.

C#:
int a; int b; string c; string d[B][I] = ""[/I][/B];
 
you must follow one rule: when you create new variable, you must assigned it!(for example: int a = 0; int b = 0; string c = "some string"; string d = null ). And when you use many if/esle it's will be better if you use switch case construction, it's makes your code shorter)
 
you must follow one rule: when you create new variable, you must assigned it!(for example: int a = 0; int b = 0; string c = "some string"; string d = null ). And when you use many if/esle it's will be better if you use switch case construction, it's makes your code shorter)

That's not true. There is no requirement to initialise a variable when it's declared. The only requirement is that you assign to the variable before using the value of the variable, i.e. you must set before you get. You can set when declaring but you can also so so any time after that but before the first get. Note that passing a variable to a method parameter declared `ref` is considered a get followed by a set, while if the parameter is declared `out` then it's only considered a set.
 
That's not true. There is no requirement to initialise a variable when it's declared. The only requirement is that you assign to the variable before using the value of the variable, i.e. you must set before you get. You can set when declaring but you can also so so any time after that but before the first get. Note that passing a variable to a method parameter declared `ref` is considered a get followed by a set, while if the parameter is declared `out` then it's only considered a set.

) yes, i know it, i write this because, if you beginner, it's common mistake, when u use unsigned variable.
 
) yes, i know it, i write this because, if you beginner, it's common mistake, when u use unsigned variable.

It certainly is a common mistake, but that's not really a good reason to provide false information.
 
Back
Top Bottom