我想檢查字符串的值。C#調用函數,但字符串爲空
但有時字符串可能包含空值。
Boolean checkValue = segment.CheckStringValue(stringValue)
當發生這種情況,然後我得到以下錯誤nullreferenceexception was unhandled
是否有辦法避免這種情況的發生?
我想檢查字符串的值。C#調用函數,但字符串爲空
但有時字符串可能包含空值。
Boolean checkValue = segment.CheckStringValue(stringValue)
當發生這種情況,然後我得到以下錯誤nullreferenceexception was unhandled
是否有辦法避免這種情況的發生?
更換
Boolean checkValue = segment.CheckStringValue(stringValue);
與
Boolean checkValue = segment.CheckStringValue(stringValue ?? String.Empty);
或處理您的方法if (value == null)
內的空值...
方法標題'CheckStringValue'會讓我相信在方法內部檢查會更好,通過檢查一個不同的字符串不會獲得太多的結果。 (拋出錯誤實際上可能是更好的事情) – Sayse
你可以試試這樣做:
String stringCheck = "";
if(stringValue != null
stringCheck=stringValue;
Boolean checkValue = segment.CheckStringValue(stringCheck);
我假設任一段爲空或stringValue爲null ..方法CheckStringValue中是否存在異常? –
'segment'對象也可以爲null! –