Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The use of unassigned local variable in c# error: What should you do?
Next
Answered
Francis Brown
  • 31
Francis Brown
Asked: May 18, 20222022-05-18T16:31:38+00:00 2022-05-18T16:31:38+00:00In: csharp

The use of unassigned local variable in c# error: What should you do?

  • 31

. Advertisement .

..3..

. Advertisement .

..4..

I get the error: use of unassigned local variable in C# when I try to run the program below:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 namespace Lab_5___Danny_Curro
 {
  class Program
  {
  static void Main(string[] args)
  {
  string firstName;
  string lastName;
  int accNumber;
  string creditPlan;
  double balance;
  string status;
  Boolean late = false;
  double lateFee;
  double monthlyCharge;
  double annualRate;
  double netBalance;
 
 
  Console.Write("Enter First Name: ");
  firstName = Console.ReadLine();
 
  Console.Write("Enter Last Name: ");
  lastName = Console.ReadLine();
 
  Console.Write("Enter Account Number: ");
  accNumber = Convert.ToInt32(Console.ReadLine());
 
 
  Console.Write("Enter Credit Card Plan Number[Blank Will Enter Plan 0]: ");
  creditPlan = Console.ReadLine();
 
  Console.Write("Enter Balance: ");
  balance = Convert.ToDouble(Console.ReadLine());
 
  Console.Write("Is This Account Late?: ");
  status = Console.ReadLine().Trim().ToLower();
 
  if (creditPlan == "0")
  {
  annualRate = 0.35; //35%
  lateFee = 0.0;
  monthlyCharge = balance * (annualRate * (1 / 12));
  return;
  }
 
  if (creditPlan == "1")
  {
  annualRate = 0.30; //30%
  if (status == "y")
  {
  late = true;
  }
 
  else if (status == "n")
  {
  late = false;
  }
  if (late == true)
  {
  lateFee = 25.00;
  }
  monthlyCharge = balance * (annualRate * (1 / 12));
  return;
  }
  if (creditPlan == "2")
  {
  annualRate = 0.20; //20%
  if (status == "y")
  {
  late = true;
  }
 
  else if (status == "n")
  {
  late = false;
  }
  if (late == true)
  {
  lateFee = 35.00;
  }
  if (balance > 100)
  {
  monthlyCharge = balance * (annualRate * (1 / 12));
  }
  else
  {
  monthlyCharge = 0;
  }
  return;
  }
  if (creditPlan == "3")
  {
  annualRate = 0.15; //15%
  lateFee = 0.00;
 
  if (balance > 500)
  {
  monthlyCharge = (balance - 500) * (annualRate * (1 / 12));
  }
  else
  {
  monthlyCharge = 0;
  }
  return;
  }
  netBalance = balance - (lateFee + monthlyCharge);
 
 
  Console.WriteLine("Name: \t\t\t {0} {1}", firstName, lastName);
  Console.WriteLine("Account Number: \t{0}", accNumber);
  Console.WriteLine("Credit Plane: \t\t{0}",creditPlan);
  Console.WriteLine("Account Late: \t\t{0}", late);
  Console.WriteLine("Balance: \t\t{0}", balance);
  Console.WriteLine("Late Fee: \t\t{0}", lateFee);
  Console.WriteLine("Interest Charge: \t{0}", monthlyCharge);
  Console.WriteLine("Net Balance: \t\t{0}",netBalance);
  Console.WriteLine("Annual Rate: \t\t{0}", annualRate);
  Console.ReadKey();
  }
  }
 }

The error appears the system notifies as follows:

Use of unassigned local variable

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

unassigned-variable
  • 2 2 Answers
  • 149 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-11T15:02:20+00:00Added an answer on June 11, 2022 at 3:02 pm

    The cause:

    At most only one of your if block can be executed, but the the compiler is not intelligent enough to notice this. And that compiler also doesn’t know the variables such as annualRate will always be assigned. Therefore, the error appears in your system.

    Solution:

    If you want this error disappears, you have to make the compiler understand easily. Below are some ways I recommend you:

    if (creditPlan == "0")
    {
     // ...
    }
    else if (creditPlan == "1")
    {
     // ...
    }
    else if (creditPlan == "2")
    {
     // ...
    }
    else
    {
     // ...
    }

    The compiler is aware of an if/else blocks guarantees a block to be executed. Therefore, if the variable is assigned in all blocks, the compiler will not give this error.

    If you want to make your code more readable, you could also use switch statements instead of ifs.

    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Hugo Chevallier
    2022-05-25T20:31:15+00:00Added an answer on May 25, 2022 at 8:31 pm

    This is how to change your declarations

    double lateFee = 0.0;
    double monthlyCharge = 0.0;
    double annualRate = 0.0;

    This error occurs because at least one of your code paths contains variables that are not set to anything.

    • 6
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.