Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Beginner Help: Unassigned local variable

Beginner Help: Unassigned local variable

Scheduled Pinned Locked Moved C#
helpcsharpcssquestioncareer
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ivelander
    wrote on last edited by
    #1

    Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem! using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }

    M T W 3 Replies Last reply
    0
    • I ivelander

      Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem! using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }

      M Offline
      M Offline
      Member 1146196
      wrote on last edited by
      #2

      The ";" at the end of code baseSalary=5000 stops the int declaration. Add "int finalSalary=0" and your code should work fine. Its a good practice to always add type "int" before variables hv fun

      I M 2 Replies Last reply
      0
      • M Member 1146196

        The ";" at the end of code baseSalary=5000 stops the int declaration. Add "int finalSalary=0" and your code should work fine. Its a good practice to always add type "int" before variables hv fun

        I Offline
        I Offline
        ivelander
        wrote on last edited by
        #3

        I knew it would be something simple. The semicolon is actually not there in my initial code, I was rearranging things after I posted it so it would be a little more clear. I think assigning 0 to finalSalary should do the trick, thanks for the help, I'm sure I will be back for more!

        1 Reply Last reply
        0
        • I ivelander

          Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem! using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }

          T Offline
          T Offline
          Tom Larsen
          wrote on last edited by
          #4

          It is entirely possible that finalSalary will not be assigned a value before it is used in execution (consider if experience is not 1, 2, or 3). This is what the compiler is complaining about. You could assign finalSalary a value right after you declare it. You could also make the else branch assign it.

          1 Reply Last reply
          0
          • I ivelander

            Hello all, I am brand new to the C# world and am having issues with my first program. Below is the code, and I am getting an "Unassigned local variable" on the Console.Write for the finalSalary variable. Is my problem with data type, or more likely the conditional? Thanks in advance for the help, and I appologize for the simplicity of my problem! using System; namespace EmployeeSalary { /// /// Calculates employee salary based on experience, birthday, position, and company success /// class EmployeeSalary { [STAThread] static void Main(string[] args) { int executive, //employee's executive status baseSalary = 5000; //employee's base salary finalSalary, //employee's final salary experience, //employee's years of experience birthday; //employee's birthday bonus // Employee's executive status (1 = exec, 2 = not exec) Console.Write( "Please enter executive code (1 = yes 2 = no): " ); executive = Int32.Parse( Console.ReadLine() ); if ( executive == 1 ) Console.WriteLine( "\nSorry, you're an executive, no benefits for you!" ); //Employee's years of experience (1 = less than 3 yrs, 2 = 3 to 5 yrs, 3 = over 5 yrs) Console.WriteLine( "\nPlease enter your experience code (1 = less than 3 yrs, 2 = 3-5 yrs, 2 = over 5 yrs)" ); experience = Int32.Parse( Console.ReadLine() ); if ( experience == 1 ) finalSalary = baseSalary - 2000; else if ( experience == 2 ) finalSalary = baseSalary + 2500; else if ( experience == 3 ) finalSalary = baseSalary + baseSalary; else Console.WriteLine("That is not a valid entry"); //Write final salary Console.WriteLine(finalSalary.ToString("c")); } } }

            W Offline
            W Offline
            whizzs
            wrote on last edited by
            #5

            Your problem is that it is possible in your if statement to never assign a value to finalSalary since you declared finalSalary at the beginning and did not initialize it. So if someone enters a "4" at your prompt the code will execute the final "else" statement and write out to the console "That is not a valid entry" but will continue on to the final Console.WriteLine which will try to execute with an uninitialized variable. Visual studio is smart enough to detect when people try to initialize variables inside of "if" statements but not smart enough to know if all paths assign a valid result to the variable so it always throws a flag when you try to do this. The answer is to do one of the following: 1) initialize the variable at instantiation 2) move the Console.WriteLine statement to inside each "if" clause that you want it to print the final salary (probably your best bet since you don't want it to write out if the users response is invalid) Hope this helps

            1 Reply Last reply
            0
            • M Member 1146196

              The ";" at the end of code baseSalary=5000 stops the int declaration. Add "int finalSalary=0" and your code should work fine. Its a good practice to always add type "int" before variables hv fun

              M Offline
              M Offline
              Matt Gerrans
              wrote on last edited by
              #6

              > Its a good practice to always add type "int" before variables. :wtf: But then it is so hard to do things like floating point math, string manipulation and all kinds of object oriented stuff. ;P Matt Gerrans

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups