For some reason I am having an error when I am trying to assign a property of nullable decimal data type to a textbox text.
For example I have this Product class:
Public Class Product
Public Property ProductId As Integer
Public Property ProductName As String
Public Property [Variant] As String
Public Property PackSize As Decimal?
End Class
and an instance of a Product has a [Variant] value of Nothing and a PackSize value of Nothing.
When I try to assign the [Variant] value to a text box like this:
VariantTextBox.Text = mProduct.[Variant]
it works fine.
But when I try to assign the PackSize value to a text box like this:
PackSizeTextBox.Text = mProduct.PackSize
it throws an exception with this message:
Nullable object must have a value.
I don't understand why this happens when I could do this:
PackSizeTextBox.Text = Nothing
with no errors whatsoever.
I tried other ways of doing things like:
PackSizeTextBox.Text = If(mProduct.PackSize, Nothing)
PackSizeTextBox.Text = If(mProduct.PackSize IsNot Nothing, mProduct.PackSize, Nothing)
PackSizeTextBox.Text = If(mProduct.PackSize.HasValue, mProduct.PackSize, Nothing)
but they all throw the same error.
However, when I tweak them a little like these:
PackSizeTextBox.Text = If(mProduct.PackSize, "")
PackSizeTextBox.Text = If(mProduct.PackSize IsNot Nothing, mProduct.PackSize, "")
PackSizeTextBox.Text = If(mProduct.PackSize.HasValue, mProduct.PackSize, "")
they weirdly work fine.
Ultimately, I really don't like doing long If statements just to get the value of a nullable data type property so I just went with this:
PackSizeTextBox.Text = mProduct.PackSize?.ToString
I hope someone could explain to me the error I am encountering. Thanks!
The Text property of a TextBox is type String so the only thing that can be assigned to it is a String. If you have Option Strict Off and assign something other than a String then the system will implicitly call ToString on it. That means that this:
PackSizeTextBox.Text = mProduct.PackSize
is basically the same as this:
PackSizeTextBox.Text = mProduct.PackSize.Value.ToString()
and it should be obvious why you get the error message you do when mProduct.PackSize is Nothing.
All of these:
PackSizeTextBox.Text = If(mProduct.PackSize, Nothing)
PackSizeTextBox.Text = If(mProduct.PackSize IsNot Nothing, mProduct.PackSize, Nothing)
PackSizeTextBox.Text = If(mProduct.PackSize.HasValue, mProduct.PackSize, Nothing)
fail to work because the If operators are basically generic, in that the two values returned MUST be the same type. As a result, all of your Nothing return values are implicitly converted to Decimal? values and you end up with that having to be implicitly converted to a String in exactly the same way with exactly the same result.
The code you ended up with:
PackSizeTextBox.Text = mProduct.PackSize?.ToString
is the correct code because you are explicitly converting something that is not a String to a String in order to assign it to a String property. That's exactly what you should be doing and exactly what you would have to do if you had Option Strict On, which you ABSOLUTELY should do.
Any VB.NET developer that has Option Strict Off by default is either a beginner who doesn't know any better or a bad developer. Turn it On now in your project properties and also in your IDE options, so that it is On by default in all future projects. You should only turn it Off when you specifically need late-binding and, even then, only in partial class files that contain only the specific code that requires late-binding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With