Error message during calculation

前端 未结 4 869
长发绾君心
长发绾君心 2021-01-29 06:31

Anyone, please take a look at my line of code.

int totalValue = 0;
    totalValue = int.Parse(Label9.Text) * int.Parse(Label6.Text);
    Label8.Text = **totalVa         


        
相关标签:
4条回答
  • 2021-01-29 07:11

    That's because totalValue is an int.

    Try this:

    Label8.Text = totalValue.ToString();
    
    0 讨论(0)
  • 2021-01-29 07:32

    Using the text directly is not a good way, what if parsing fails?

    Use

    int? val1=GetInt32(Label9.Text);
    int? val2=GetInt32(Label6.Text);
    
    if(val1!=null&&val2!=null)
    {
    int totalValue = 0;
        totalValue = val1+val2;
    Label8.Text = totalValue.ToString();
    }
    //You can also write your own logic on the TextBoxs if they did not contain a valid value by checking if val1 or val2 are null or not
    

    Using the function to return int value if the input can be converted.

      public  int? GetInt32(string s)
        {
            int i;
            if (Int32.TryParse(s, out i)) return i;
            return null;
        }
    
    0 讨论(0)
  • 2021-01-29 07:35

    try this:

    int totalValue = 0;
    totalValue = int.Parse(Label9.Text) * int.Parse(Label6.Text);
    Label8.Text = totalValue.ToString();
    
    0 讨论(0)
  • 2021-01-29 07:36

    You should convert int to string. Something like this:

    Label8.Text = totalValue.ToString();
    

    Or this:

    Label8.Text = totalValue + "";
    
    0 讨论(0)
提交回复
热议问题