无法将类型'int'隐式转换为'string' [英] Cannot implicitly convert type 'int' to 'string'

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

问题描述

我正在编写Listbox的代码,用于计算ListBox中当前的总数值的总和。
这样的
i geting错误 - 不能隐式地将类型'int'转换为'string'

错误显示此行 - TotalFees.Text =总计;

我的ListBox值是这种类型

500

1000

1500

2000





帮助我任何人



我尝试过:



I am write the code for Listbox for calculating the sum of Total Numeric Values in present in ListBox.
i geting error like this-Cannot implicitly convert type 'int' to 'string'
Error shows here this line- TotalFees.Text = Total;
My ListBox values are this type
500
1000
1500
2000


help me anyone

What I have tried:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       int Total;
       foreach (string Str in ListBox1.Items)
       {
           Total = (Total + int.Parse(Str));
       }

       TotalFees.Text = Total;
   }

推荐答案

TotalFees.Text = Total.ToString();


解决这个问题很容易:

It is quite easy to solve this:
TotalFees.Text = Total.ToString();


int Total = 0; // set Total to zero to start
foreach (string Str in ListBox1.Items)
{
    Total = (Total + int.Parse(Str));
}



首次使用前需要初始化Total。只需修改上面的代码。


You need to initialise Total before using it the first time. Just modify the code as above.


这篇关于无法将类型'int'隐式转换为'string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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