title: Assigning textbox text with property of nullable datatype
body: <p>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.</p>
  
  <p>For example I have this Product class:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>and an instance of a <code>Product</code> has a <code>[Variant]</code> value of <code>Nothing</code> and a <code>PackSize</code> value of <code>Nothing</code>.</p>
  
  <p>When I try to assign the <code>[Variant]</code> value to a text box like this:</p>
  
  <pre><code>VariantTextBox.Text = mProduct.[Variant]
  </code></pre>
  
  <p>it works fine.</p>
  
  <p>But when I try to assign the <code>PackSize</code> value to a text box like this:</p>
  
  <pre><code>PackSizeTextBox.Text = mProduct.PackSize
  </code></pre>
  
  <p>it throws an exception with this message:</p>
  
  <blockquote>
    <p>Nullable object must have a value.</p>
  </blockquote>
  
  <p>I don't understand why this happens when I could do this:</p>
  
  <pre><code>PackSizeTextBox.Text = Nothing
  </code></pre>
  
  <p>with no errors whatsoever.</p>
  
  <p>I tried other ways of doing things like:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>but they all throw the same error.</p>
  
  <p>However, when I tweak them a little like these:</p>
  
  <pre><code>PackSizeTextBox.Text = If(mProduct.PackSize, "")
  PackSizeTextBox.Text = If(mProduct.PackSize IsNot Nothing, mProduct.PackSize, "")
  PackSizeTextBox.Text = If(mProduct.PackSize.HasValue, mProduct.PackSize, "")
  </code></pre>
  
  <p>they weirdly work fine.</p>
  
  <p>Ultimately, I really don't like doing long <code>If</code> statements just to get the value of a nullable data type property so I just went with this:</p>
  
  <pre><code>PackSizeTextBox.Text = mProduct.PackSize?.ToString
  </code></pre>
  
  <p>I hope someone could explain to me the error I am encountering.
  Thanks!</p>