Cannot implicitly convert type 'string' to 'double' Issue

前端 未结 5 1451
别那么骄傲
别那么骄傲 2020-12-20 10:17
    private void updateButton_Click(object sender, EventArgs e)
    {
        //custID.Text = customers[id].ID.ToString();
        customers[id].Name = custName.Text         


        
相关标签:
5条回答
  • 2020-12-20 10:18

    You should use Double.TryParse() to convert your string to a Double.You cannot save a string on a Double Datatype and there is no implicit conversion available in c# to do that

    Double result;
    Double.TryParse(custBal.Text,out result);
    
    0 讨论(0)
  • 2020-12-20 10:23

    Assuming you know they contain a properly formatted number, what you want is to parse the numeric value contained in each of your strings and assign it to a double type variable (which involves transforming a representation of your number as a string into another of different binary content: a double), not cast it (which doesn't). Try:

    customers[id].Balance = Double.Parse(custBal.Text);
    customers[id].Year_used =  Double.Parse(custYear.Text);
    

    Or, if you want to test the parsing's success against a returned boolean instead of doing exception handling to test for a FormatException, you can use TryParse instead:

    if (!Double.TryParse(custBal.Text, customers[id].Balance))
       Console.WriteLine("Parse Error on custBal.Text");
    if (!Double.TryParse(custYear.Text, customers[id].Year_used))
       Console.WriteLine("Parse Error on custYear.Text");
    

    More info:

    http://msdn.microsoft.com/en-us/library/t9ebt447%28v=vs.80%29.aspx

    http://msdn.microsoft.com/en-us/library/994c0zb1.aspx

    0 讨论(0)
  • 2020-12-20 10:24

    You cannot assign string to a double directly. You need to parse strings to assign them to doubles, like this:

    customers[id].Balance = double.Parse(custBal.Text);
    customers[id].Year_used = double.Parse(custYear.Text);
    
    0 讨论(0)
  • 2020-12-20 10:26

    The best way to convert 'String' to 'Double' is by using TryParse, as shown below:

    int result;
    
    if (double.TryParse(custBal.Text, out result))
        customers[id].Balance = result;
    if (double.TryParse(custYear.Text, out result))
        customers[id].Year_used = result;
    
    0 讨论(0)
  • 2020-12-20 10:41

    Your user is entering a balance and is typing text, so how do you know the text can be represented by a number? C# will not let you convert a string to a double implicitely because there is no unique way to do that without make substantial assumptions about the string and implicit conversions should never throw an exception (Framework Design Guidelines), so it is best to not offer implicit conversion at all.

    For example, since it is a balance, what if the user types "(100.25)" or "-100.25" or "-$100.25" or "-€100,25" or "negative one hundred and twenty-five cents" All of those are valid strings, so how would you convert them to a double?

    The answer is that there is not one correct answer: you can map strings to double in any way that makes sense to you. You could, of course, write your own function of the form Func<String, double> but there are functions provided by the .Net framework that implement the most common and intuitive conversions.

    Others have posted double.Parse and double.TryParse so I want to add to make sure also to look into NumberStyles and IFormatProvider if necessary.

    0 讨论(0)
提交回复
热议问题