selectedItem
has two fields:
int? _cost
string _serialNumber
In this example, _cost
and _serialNumber
of selectedItem
are BOTH null. I am reading through the fields of selectedItem
via their properties, and filling in textboxes with their values, when...
TextBox1.Text = selectedItem.Cost.ToString(); //no error
TextBox2.Text = selectedItem.SerialNumber.ToString(); //error
I understand that SerialNumber.ToString()
is redundant (because it is already a string), but I don't understand why this causes this exception:
Nullable object must have a value.
int? _cost
is nullable, and does not have a value, yet it does not give me the exception.
string _serialNumber
is nullable, and does not have a value, yet it does give me the exception.
This question touches on it, the guy is essentially asking the same thing, but there is no designated answer, and it also doesn't explain why a nullable int
? For example, can I use .ToString()
on a nullable int but not on a null string?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…