Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
c# switch function error, maybe a misunderstanding of the format?

hello I tried using this code to create a mini currency converter for my assignment but I don't understand why it doesn't work, please let me know why it might be so, thanks.

the error says that newValue has already been defined.

switch (userCurrency)
                {
                    case "USD":
                        double newValue = userEuro * usd;
                        break;

                    case "GBP":
                        double newValue = userEuro * gbp;
                        break;

                    case "CHF":
                        double newValue = userEuro * chf;
                        break;

                    case "AUD":
                        double newValue = userEuro * aud;
                        break;

                    case "CAD":
                        double newValue = userEuro * cad;
                        break;
                    default:
                        Console.WriteLine("Invalid input!");
                        break;

                }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Instead of declaring newValue inside every case: declare it once (without assigning a value) above the switch, and just assign a value inside each case.

Or maybe use a "switch expression" instead.

over 4 years ago · Santiago Trujillo Report

0

define "newValue" before the switch statement:

double newValue;
switch (userCurrency)
{
   case "USD":
     newValue = userEuro * usd;
   break;
...
over 4 years ago · Santiago Trujillo Report

0

Extract the newValue declaration outside of the switch case block scope and then assign it inside the different switch cases

double newValue;
switch (userCurrency)
{
    case "USD":
       newValue = userEuro * usd;
       break;

    case "GBP":
        newValue = userEuro * gbp;
        break;

    case "CHF":
        newValue = userEuro * chf;
        break;

    case "AUD":
        newValue = userEuro * aud;
        break;

    case "CAD":
        newValue = userEuro * cad;
        break;
    default:
        Console.WriteLine("Invalid input!");
        break;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!