C# 无法隐式转换类型';字符串';至';双倍';问题

C# 无法隐式转换类型';字符串';至';双倍';问题,c#,forms,C#,Forms,似乎我在尝试运行代码时遇到了此错误??问题是,我把所有的int都改成了double 以及: private void updateButton_Click(object sender, EventArgs e) { //custID.Text = customers[id].ID.ToString(); customers[id].Name = custName.Text.ToString(); customers[id].Subu

似乎我在尝试运行代码时遇到了此错误??问题是,我把所有的int都改成了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;
    }

}


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

}

custBal的正确代码是什么。而且是今年。让它显示在我的表格上?有什么想法吗?

您应该使用
Double.TryParse()
将字符串转换为Double。您不能将字符串保存在Double数据类型上,而且c中没有隐式转换

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

您应该使用
Double.TryParse()
将字符串转换为Double。您不能在Double数据类型上保存字符串,而且c#中没有隐式转换可用于此转换

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

假设您知道它们包含一个格式正确的数字,那么您需要的是解析每个字符串中包含的数值,并将其分配给一个双精度类型变量(这涉及将数字的表示形式转换为另一个不同二进制内容的字符串:双精度),而不是强制转换它(不转换)。尝试:

或者,如果要根据返回的布尔值测试解析是否成功,而不是执行异常处理以测试FormatException,则可以改用TryParse:

customers[id].Balance = Double.Parse(custBal.Text);
customers[id].Year_used =  Double.Parse(custYear.Text);
更多信息:


假设您知道它们包含格式正确的数字,您需要的是解析每个字符串中包含的数值,并将其分配给双精度类型变量(这涉及将数字的表示形式转换为另一个不同二进制内容的字符串:双精度),而不是强制转换(不需要)。尝试:

或者,如果要根据返回的布尔值测试解析是否成功,而不是执行异常处理以测试FormatException,则可以改用TryParse:

customers[id].Balance = Double.Parse(custBal.Text);
customers[id].Year_used =  Double.Parse(custYear.Text);
更多信息:


您不能直接将
字符串
分配给
双精度
。您需要解析字符串以将其分配给double,如下所示:

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");

不能将
字符串
直接分配给
双精度
。您需要解析字符串以将其分配给double,如下所示:

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");

将“String”转换为“Double”的最佳方法是使用TryParse,如下所示:

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

将“String”转换为“Double”的最佳方法是使用TryParse,如下所示:

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

您的用户正在输入余额并键入文本,那么您如何知道文本可以用数字表示?C#不允许您隐式地将字符串转换为double,因为如果不对字符串进行实质性假设,就没有唯一的方法可以这样做,并且隐式转换永远不会引发异常(框架设计指南),因此最好不要提供隐式转换

例如,由于它是一个余额,如果用户键入“(100.25)”或“-100.25”或“-$100.25”或“-100,25”或“负125美分”所有这些都是有效字符串,那么如何将它们转换为双精度字符串呢

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


其他人发布了
double.Parse
double.TryParse
,因此我想添加,以确保在必要时查看
numberstyle
IFormatProvider

您的用户正在输入余额并键入文本,那么您如何知道文本可以用数字表示?C#不允许您隐式地将字符串转换为double,因为如果不对字符串进行实质性假设,就没有唯一的方法可以这样做,并且隐式转换永远不会引发异常(框架设计指南),因此最好不要提供隐式转换

例如,由于它是一个余额,如果用户键入“(100.25)”或“-100.25”或“-$100.25”或“-100,25”或“负125美分”所有这些都是有效字符串,那么如何将它们转换为双精度字符串呢

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


其他人发布了
double.Parse
double.TryParse
,因此我想添加,以确保在必要时查看
numberstyle
IFormatProvider

double to try parse需要两个参数double to try parse需要两个参数您也可以去掉ID、名称和属性分配上的.ToString()。您正在将字符串(.Text)指定给字符串属性。还要考虑一下验证——如果没有有效的余额和年份就不可能提交表单,那么就可以了。但是,另外请看一下。您也可以去掉ID、名称和郊区分配上的.ToString()。您正在将字符串(.Text)指定给字符串属性。还要考虑一下验证——如果没有有效的余额和年份就不可能提交表单,那么就可以了。不过还是看一看吧。谢谢!使用相同的代码,当我更改表单上的信息时,它似乎不会更新任何更改。当我运行程序时,当我单击“更新”按钮并单击“下一步”时,表单上的更改似乎不会更改。有什么想法吗?谢谢!使用相同的代码,当我更改表单上的信息时,它似乎不会更新任何更改。当我运行程序时,当我单击“更新”按钮并单击“下一步”时,表单上的更改似乎不会更改。有什么想法吗?干杯。是的,谢谢你的帖子。是的,不,我知道字符串不能转换为双精度。我没怎么想哈哈谢谢你!干杯是的谢谢你的帖子是的不我知道