ToNullableString()
<Extension()> _ Public Function ToNullableString(Of T As Structure)(ByVal param As Nullable(Of T)) As String If (Not param.HasValue) Then Return String.Empty Else Return param.Value.ToString End If End FunctionExample:
Dim value as System.Nullable(Of Byte) = GetValueFromDatabase() Textbox1.Text = value.ToNullableString() ' GetValueFromDatabase is simulating retrieving a System.Nullable(Of Byte) value from the database.
Description
Calling Value.ToString on a Nullable(Of T) type where the value is null will result in an "Nullable object must have a value." exception being thrown. This extension method can be used in place of .ToString to prevent this exception from occurring.
Details
- Author: Chris Rock
- Submitted on: 3/31/2008 6:23:54 PM
- Language: VB
- Type: System.Nullable<T>
- Views: 2106
Double click on the code to select all.