【发布时间】:2014-03-23 05:12:23
【问题描述】:
转换问题
对不起,如果这是一个愚蠢的问题,但我是 C# 的初学者。我正在尝试制作一个简单的时间表助手,但在指示的代码行下出现错误:
private void buttonShow_Click(object sender, EventArgs e)
{
try
{
int val = Convert.ToInt32(textBoxVal.Text);
for (int i = 1; i < 13; i = i + 1)
{
listBoxTimes.Items.Add((i * val).ToString()); //'This is the line'//
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
textBoxVal.Text = "";
listBoxTimes.Items.Clear();
}
}
这是错误信息:
错误 1 'System.Windows.Forms.ListBox.ObjectCollection.Add(object)' 的最佳重载方法匹配有一些无效参数 C:\Users\Chris\Documents\Visual Studio 2012\Projects\sharptimestable\sharptimestable\ Form1.cs 27 21 尖锐时间稳定
而这个已经结束了.ToString的转换:
错误 2 参数 1:无法从“方法组”转换为“对象”C:\Users\Chris\Documents\Visual Studio 2012\Projects\sharptimestable\sharptimestable\Form1.cs 27 44 sharptimestable
【问题讨论】:
-
让我猜猜你刚刚更正了“.ToString);”中错误行末尾的代码到“.ToString());”并且在进行更正后您还没有重新编译。
-
整个 try-catch-finally 代码应该从此代码中删除。一般这段代码不会引起任何异常,只要有错误就修复它们。
-
@MrFox
Convert.ToInt32可以轻松抛出异常。
标签: c# .net string type-conversion