Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Switch value outside of function?

I'm trying to get the value alpha and beta displayed OUTSIDE a switch, but the compiler throws Compiler Warning (level 2) CS0162 (Unreachable code detected). Any help is greatly appreciated.

        private void TestButton_Click(object sender, EventArgs e)
        {
            string alpha;
            byte beta = 0;

            alpha = comboBoxAlpha.Text;

            switch (alpha)
            {
                case "charlie":
                    beta = 1;
                    return;

                default:
                    beta = 0;
                    return;
            }

            if (beta == 0)
            {
                label1.Text = alpha;
            }
            else label1.Text = beta;
        }
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There's no need in that complex code. As fas as I can see, you want "1" on "cahalie" and comboBoxAlpha.Text otherwise.

private void TestButton_Click(object sender, EventArgs e) {
  if (comboBoxAlpha.Text == "charlie")
    label1.Text = "1";
}

Or even

private void TestButton_Click(object sender, EventArgs e) =>
  label1.Text = comboBoxAlpha.Text == "charlie" ? "1" : comboBoxAlpha.Text;

The problem in your code is in return instead of break: returns return from the method, when break just breaks the switch:

    private void TestButton_Click(object sender, EventArgs e)
    {
        string alpha;
        byte beta = 0;

        alpha = comboBoxAlpha.Text;

        switch (alpha)
        {
            case "charlie":
                beta = 1;
                break;    // Just break (jump out of) switch

            default:
                beta = 0;
                break;    // Just break (jump out of) switch 
        }

        if (beta == 0)
        {
            label1.Text = alpha;
        }
        else label1.Text = beta;
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda