Convert string to integer

Asked

Viewed 1,159 times

3

I can’t convert my text TextBox in one piece.

namespace Pag98_Exe1
{
    public partial class frmveiculos : Form
    {
        public frmveiculos()
        {
            InitializeComponent();
        }

        Veiculos veiculos = new Veiculos(); //Declarando a classe veiculos

        private void btnenviar_Click(object sender, EventArgs e)
        {
            if(mskTxtplaca.Text != "" && cbxmarca.Text != "" && txtanofabricacao.Text != "")
            {
                veiculos.Placa = mskTxtplaca.Text; //Atribuindo o valor de Placa ao texto do TextBox
                veiculos.Marca = cbxmarca.Text;
                veiculos.AnoFabricacao(int.Parse(txtanofabricacao.Text)); //Passando o valor do TextBox para inteiro e atribuindo ao TextBox
            }
        }
    }
}
  • 2

    What’s the matter?

  • @bigown Gravity Code Description Project File Line Deletion State Error CS0029 Cannot implicitly convert type "string" to "int" Pag98_exe1 C: Users home Desktop Jobs COTEMIG Pag98_exe1 Pag98_exe1 28 Active

  • @bigown Error line is 3rd within the if condition

  • Already tried Convert.toInt32() ?

2 answers

4


Since the text comes from a control that the user can type there is no guarantee that the text can be converted to integer, so you have to try to do, if it works out do what you want, otherwise you need to treat it in some way, issue an error, put a default value, something like that. This is done with the method TryParse(). Other solutions are failures, do not use unless you are sure that the value can be converted, which does not seem to be the case. I have my doubts whether the rest of the logic is adequate, but I cannot speak without knowing the requirements.

private void btnenviar_Click(object sender, EventArgs e) {
    if(mskTxtplaca.Text != "" && cbxmarca.Text != "" && txtanofabricacao.Text != "") {
        veiculos.Placa = mskTxtplaca.Text;
        veiculos.Marca = cbxmarca.Text;
        int anoFabricacao;
        if (int.TryParse(txtanofabricacao.Text, out anoFabricacao) {
            veiculos.AnoFabricacao = anoFabricacao;
        } else {
            //aqui coloque o que deve fazer se a conversão falhar
        }
    }
}

In C# 7 can make it a little simpler:

private void btnenviar_Click(object sender, EventArgs e) {
    if(mskTxtplaca.Text != "" && cbxmarca.Text != "" && txtanofabricacao.Text != "") {
        veiculos.Placa = mskTxtplaca.Text;
        veiculos.Marca = cbxmarca.Text;
        if (int.TryParse(txtanofabricacao.Text, out var anoFabricacao) {
            veiculos.AnoFabricacao = anoFabricacao;
        } else {
            //aqui coloque o que deve fazer se a conversão falhar
        }
    }
}

I put in the Github for future reference.

  • Very good explanation, taking advantage, you can tell me how to leave certain spaces of a masked text box to receive only one type of character ? Ex: Vehicle registration plate (AAA-0000) the part where the letters are only given letters and the numbers are only numbers

  • @Caiovieira take a look at [tour], we are different from a forum, so every question needs to be in a different question so that it can be evaluated independently by everyone and that can help other people who have the same problem. Nor could I respond appropriately in a short comment and with low formatting capacity. But it seems to be on the right track in using the {MaskedTextBox](https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox(v=vs.110). aspx)

  • Thank you for the strength there

2

Your post is a little confused, so what I understood from the code tries this solution:

veiculos.AnoFabricacao = int.Parse(txtanofabricacao.Text);
  • It worked my friend, thanks for your help

  • accept the answer ai

  • Not for another five minutes.

Browser other questions tagged

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