Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

c# - Cannot implicitly convert type 'string' to 'double' Issue

    private void updateButton_Click(object sender, EventArgs e)
    {
        //custID.Text = customers[id].ID.ToString();
        customers[id].Name = custName.Text.ToString();
        customers[id].Suburb = custSuburb.Text.ToString();
        customers[id].Balance = custBal.Text;
        customers[id].Year_used = custYear.Text;
    }

}


public class Customer
{
    protected string id;
    protected string name;
    protected string suburb;
    protected double balance;
    protected double year_used;

    public string ID
    {
        get { return id; }
    }

    public string Name
    {
        get { return name; }
        set { value = name; }
    }

    public string Suburb
    {
        get { return suburb; }
        set { value = suburb; }
    }

    public double Balance
    {
        get { return balance; }
        set { value = balance; }
    }

    public double Year_used
    {
        get { return year_used; }
        set { value = year_used; }
    }


    public Customer(string id, string name, string suburb, double balance, double year_used)
    {
        this.id = id;
        this.name = name;
        this.suburb = suburb;
        this.balance = balance;
        this.year_used = year_used;
    }

}

Seems that i get this error when i try run the code?? what seems to be the issue i changed everything that had int to double.

As well as:

        customers[id].Balance = custBal.Text;
        customers[id].Year_used = custYear.Text;

What would be the proper code for the custBal. and custYear. to make it display on my form? any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...