轉換問題
對不起,如果這是一個愚蠢的問題,但我是一個初學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 sharptimestable
和這一次是在.ToString
轉換:
錯誤2參數1:不能從 '方法組' 到 '對象' C轉換:\用戶\克里斯\文件\的Visual Studio 2012 \項目\ sharptimestable \ sharptimestable \ Form1.cs中27 44 sharptimestable
讓我猜一猜,你剛纔糾正代碼從」的ToString故障線路)的末尾; 「到「.ToString());」並且在做出更正之後你還沒有重新編譯過。 – Tormod
應該從這段代碼中刪除整個try-catch-finally代碼。通常情況下,這段代碼不會引發任何異常,只要有錯誤就修復它們。 – MrFox
@MrFox'Convert.ToInt32'''asasily_拋出一個異常。 –