所以我有这些代码.如何将cb和cu这两个字符串加在一起?我已经尝试过了
So i have these codes. How do i add the two strings, cb and cu together? I've tried doing
decimal totalprice; totalprice = cu + cb;,但是它表示该名称在上下文中不存在. 我该怎么办?
but it says the name does not exist in the context. What should i do??
我正在使用Windows形式btw
i'm using windows form btw
推荐答案您在这里遇到几个问题:
You have several issues here:
首先,您的string cu在if范围内声明.它在该范围之外将不存在.如果需要在if范围之外使用它,请在外部声明它.
First of all, your string cu is declared inside the if scope. It will not exist outside that scope. If you need to use it outside the scope of the if, declare it outside.
第二,数学运算不能应用于string.为什么将数字值转换为字符串?您的代码应为:
Second, math operations cannot be applied to strings. Why are you casting your numeric values to string? Your code should be:
decimal totalamtforcushion = 0m; if (cushioncheckBox.Checked) { totalamtforcushion = 63m * cushionupDown.Value; //string cu = totalamtforcushion.ToString("C"); You don't need this cushioncheckBox.Checked = false; cushionupDown.Value = 0; } decimal totalamtforcesarbeef = 0m; if (cesarbeefcheckBox.Checked) { totalamtforcesarbeef = 1.9m * cesarbeefupDown.Value; //string cb = totalamtforcesarbeef.ToString("C"); you don't need this either cesarbeefcheckBox.Checked = false; cesarbeefupDown.Value = 0; } var totalprice = totalamtforcushion + totalamtforcesarbeef;更多推荐
如何将字符串添加在一起?
发布评论