不能将类型'字符串'隐式转换为'double'问题 [英] Cannot implicitly convert type 'string' to 'double' Issue

查看:254
本文介绍了不能将类型'字符串'隐式转换为'double'问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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;
}

}


公共类客户
{
保护字符串ID;
受保护的字符串名称;
受保护的字符串郊区;
保护双倍余额;
保护double year_used;

公共字符串ID
{
get {return id; }
}

公共字符串名称
{
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; }


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

}

似乎我在得到这个错误时我尝试运行代码?似乎是什么问题,我改变了所有的内容翻倍。



以及:

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

custBal的正确代码是什么。和custYear。使其显示在我的表单上?任何想法?

解决方案

您的用户正在输入一个余额并输入文本,那么您如何知道文本可以用一个号码? C#不会让您将字符串转换为双重隐式,因为在没有对字符串做出实质性假设的情况下,没有独特的方法可以做到这一点,并且隐式转换不应该抛出异常(框架设计指南),所以最好不要提供隐式转换。



例如,因为它是一个余额,如果用户输入(100.25)或-100.25或 - $ 100.25或 - €100,25或负一百二十五美分所有这些都是有效的字符串,那么如何将它们转换为双精度?



答案是,没有一个正确的答案:你可以映射字符串以任何对你有意义的方式加倍。当然,您可以编写自己的函数形式为 Func< String,double> ,但是.Net框架提供的函数实现了最常见和直观的转换。

其他人发布了 double.Parse double.TryParse 因此,如果有必要,我想添加以确保也查看 NumberStyles IFormatProvider

    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?

解决方案

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.

这篇关于不能将类型'字符串'隐式转换为'double'问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆