Cannot implicitly Convert type 'string' to 'int'

Asked

Viewed 1,521 times

1

protected void btnSave_Click(object sender, EventArgs e)
    {
        List<ProdutoAmortizacaoCreditoDias> aDay = new List<ProdutoAmortizacaoCreditoDias>();
        ProdutoAmortizacaoCreditoDias ee = new ProdutoAmortizacaoCreditoDias();


        if (ProcessingType == 1)
        {


            ee.Dia = this.txtDay.Text;        

        }
        else
        {
            ee.Dia = this.lblDay.Text;
            ee.FromProductID = this.hdnSourceID.Value;
            ee.ToProductID = this.hdnDestinationID.Value;
        }

The mistake is in:

ee.Dia = this.txtDay.Text;

Error:

Cannot implicitly Convert type 'string' to 'int'

I realize you can’t convert from string for int.

How can I turn this around?

  • What is the type of ee.Day? Put what it does ToInt32Nullable(). What is the data that is trying to convert that gives this error?

  • The data is int Dia, I’m trying to convert this because the goal is to fill the data and then save toInt32Nullable(), this wrong, I know.. instead of toint is text.

3 answers

5


You have to have the conversion done. As the data may not be valid you have to test to see if the conversion has run correctly. This is done with TryParse().

protected void btnSave_Click(object sender, EventArgs e) {
    var aDay = new List<ProdutoAmortizacaoCreditoDias>();
    var ee = new ProdutoAmortizacaoCreditoDias();
    int dia;
    if (int.TryParse(this.txtDay.Text, out dia)) {
        ee.Dia = ee.Dia = dia;
    } else {
        //faz alguma coisa aqui porque deu erro na conversão.
    }
    if (ProcessingType != 1) {
        ee.FromProductID = this.hdnSourceID.Value;
        ee.ToProductID = this.hdnDestinationID.Value;
    }

I put in the Github for future reference.

In C# 7 you can do only:

protected void btnSave_Click(object sender, EventArgs e) {
    var aDay = new List<ProdutoAmortizacaoCreditoDias>();
    var ee = new ProdutoAmortizacaoCreditoDias();
    if (int.TryParse(this.txtDay.Text, out var dia)) {
        ee.Dia = ee.Dia = dia;
    } else {
        //faz alguma coisa aqui porque deu erro na conversão.
    }
    if (ProcessingType != 1) {
        ee.FromProductID = this.hdnSourceID.Value;
        ee.ToProductID = this.hdnDestinationID.Value;
    }

I took the opportunity to give an organized in the code.

2

  • It worked! Thank you.. I am using ASP.NET it worked

  • If the data is typed wrong this gives error.

  • True, @bigown. Before converting you must ensure that lblDay contains only digits

  • Will it be a mistake? What do you mean?

  • if user type in field lblDay something like "text", will give error in conversion. It is worth using tryparse (https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx ) as spoken in another answer here

0

Probably your Day Property is of type Int, when you take the value this.lblDay.Text; the default text is a string, so you cannot assign a string to an Int type, you would have to convert before the following way:

ee.Dia =  Convert.ToInt32(this.lblDay.Text);

OR

ee.Dia =  int.Parse(this.lblDay.Text);

I believe it will work, but it is good to validate being this field is really an integer value, you can do this with the example int.Tryparse HERE

  • If the data is typed wrong this gives error.

Browser other questions tagged

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