如何将字符串添加在一起?

编程入门 行业动态 更新时间:2024-10-28 18:36:50
本文介绍了如何将字符串添加在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public void button1_Click(object sender, EventArgs e) { if (cushioncheckBox.Checked) { decimal totalamtforcushion = 0m; totalamtforcushion = 63m * cushionupDown.Value; string cu = totalamtforcushion.ToString("C"); cushioncheckBox.Checked = false; cushionupDown.Value = 0; } if (cesarbeefcheckBox.Checked) { decimal totalamtforcesarbeef = 0m; totalamtforcesarbeef = 1.9m * cesarbeefupDown.Value; string cb = totalamtforcesarbeef.ToString("C"); cesarbeefcheckBox.Checked = false; cesarbeefupDown.Value = 0; } }

所以我有这些代码.如何将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;

更多推荐

如何将字符串添加在一起?

本文发布于:2023-10-19 20:39:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何将

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!