如何将字符串添加在一起? [英] how to add strings together?

查看:90
本文介绍了如何将字符串添加在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 cuif范围内声明.它在该范围之外将不存在.如果需要在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;

这篇关于如何将字符串添加在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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