How do I play the variable within a text in "Messagebox.Show" of a "case" on the "switch" command?

Asked

Viewed 224 times

2

I’m doing a program interface on switch using C# to find out what a couple’s wedding anniversary is, only that I do not know how to insert the result variable for the case default if the marriage is not 25, 50 and 75 (type can be 21, 32, 45 years of marriage). What I must do to show the value in this case if it does not meet the previous conditions of 25, 50 and 75 years of married.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TesteSwitch1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            int resultado, ano_atual, ano_casaram;
            ano_atual = Convert.ToInt32(txtAA.Text);
            ano_casaram = Convert.ToInt32(txtAC.Text);
            resultado = ano_atual - ano_casaram;
            switch (resultado)
            {
                case 25:
                    MessageBox.Show("25 anos de casado! BODAS DE PRATA!", "Mensagem");
                break;
                case 50:
                    MessageBox.Show("50 anos de casado! BODAS DE OURO!", "Mensagem");
                break;
                case 75:
                    MessageBox.Show("75 anos de casado! BODAS DE DIAMANTE!", "Mensagem");
                break;
                default:
                    MessageBox.Show("", "Mensagem"); // Aqui que está a minha dúvida
                break;
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            txtAA.Text = "";
            txtAC.Text = "";
        }
    }
}

1 answer

3


I used interpolation to get what you want, and I took advantage and fixed another error making the validation of the input correctly preventing your application break, but need to improve the treatment, then stay with you.

private void Button1_Click(object sender, EventArgs e) {
    if (!int.TryParse(txtAA.Text, out var anoAtual || !int.TryParse(txtAC.Text, out var anoAtual) {
        MessageBox.Show("Ano inválido não é possível continuar", "Mensagem");
        return;
    }
    int resultado = anoAtual - anoCasamento;
    switch (resultado) {
    case 25:
        MessageBox.Show("25 anos de casado! BODAS DE PRATA!", "Mensagem");
        break;
    case 50:
        MessageBox.Show("50 anos de casado! BODAS DE OURO!", "Mensagem");
        break;
    case 75:
        MessageBox.Show("75 anos de casado! BODAS DE DIAMANTE!", "Mensagem");
        break;
    default:
        MessageBox.Show($"{resultado} anos de casado", "Mensagem"); // Aqui que está a minha dúvida
    }
}

I put in the Github for future reference.

Note that this has nothing to do with Visual Studio (which had in the original version of the question, with switch or even with the MessageBox.Show(), the problem is only of manipulation of string.

  • I was giving error, I looked well and it was :). But why that invalid year if before the code?

  • I think this can help you https://answall.com/q/16089/101 This too: https://answall.com/q/90981/101

  • Remember: you need C# 6.0+ to have Interpolated Strings support.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.