Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

define "newValue" before the switch statement:

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda