0
所以我昨天寫了this question。我仍在使用更新下的解決方案,但由於某些原因,我現在收到FormatException was Unhandled
錯誤。在該錯誤下,編譯器窗口顯示Input string was not in a correct format
。爲什麼會發生這種情況?FormatException將字符串轉換爲Int32時未處理
當我看到這個錯誤時,我想我可能會用Int32.TryParse
運氣好一些,比如this link。但它幾乎是同樣的交易。
這是我目前有...
//Initializing a parent TreeView Item
TreeViewItem parentItem = (TreeViewItem)SelectedItem.Parent;
//This is the call to getNumber that I am having trouble with.
//It is located in an if statement, but I didn't bother to write out the
//whole statement because I didn't want to add surplus code
int curNumber = getNumber(parentItem.Header.ToString());
//Gets the number contained in a Node's header
public static int getNumber(string parentNodeHeader)
{
int curNumber = 0;
curNumber = Convert.ToInt32(parentNodeHeader); //**FormatException!!
return curNumber;
}
注意:我點擊,使這個錯誤出現,這並不在他們數值節點。但是,他們的父母會這麼做(這是我不明白的,因爲我將父母的header
傳遞給函數)。
感謝您的幫助!
好吧,照顧了例外。但'Int32.TryParse'失敗,因爲'curNumber'返回0.有什麼我可以告訴你,這將有助於你解釋爲什麼? –
@Ericafterdark好吧,你應該嘗試緩慢的調試,看看有什麼在parentItem,然後在parentItem.Header ... –
那麼我的父節點的頭設置爲'TextBlock',這是一個問題嗎?在if語句中,我得到了'parentItem'的正確值,但是當它傳遞給'getNumber'時,'parentNodeHeader'顯示''System.Windows.Controls.TextBlock'',因爲它是Value。 –