Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a generic method for converting string to nullable numbers

I have 3 similar functions, that only change based on numeric type:

<Extension()> _
Public Function ToNullableShort(ByVal str As String) As Short?
    Dim intReturn As Short
    If Short.TryParse(str, intReturn) Then
        Return intReturn
    Else
        Return Nothing
    End If
End Function

<Extension()> _
Public Function ToNullableByte(ByVal str As String) As Byte?
    Dim intReturn As Byte
    If Byte.TryParse(str, intReturn) Then
        Return intReturn
    Else
        Return Nothing
    End If
End Function

<Extension()> _
Public Function ToNullableLong(ByVal str As String) As Long?
    Dim intReturn As Long
    If Long.TryParse(str, intReturn) Then
        Return intReturn
    Else
        Return Nothing
    End If
End Function

I was trying to make a generic function out of this, but couldn't do it. How can I make a generic function that will take a specific T and make it a Nullable(of T)?

like image 625
Steve Wright Avatar asked Jun 19 '09 18:06

Steve Wright


2 Answers

There's an identical question here (in C#): Convert string to nullable type (int, double, etc...)

Public Function ConvertString(Of T As Structure)(ByVal value As String) As System.Nullable(Of T)
    Try
        Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
    Catch
        Return Nothing
    End Try
End Function
like image 186
jjxtra Avatar answered Nov 15 '22 13:11

jjxtra


How about:

Public Shared Function Parse(Of T As Structure)(ByVal value As String) As T?
    If String.IsNullOrEmpty(value) Then
        Return Nothing
    End If
    Return New T?(DirectCast(TypeDescriptor.GetConverter(GetType(T)).ConvertFromInvariantString(value), T))
End Function

(note I used reflector to translate that from the C# below)

You could add try/catch around the ConvertFromInvariantString, but to be honest I'd rather have the code throw an exception if I give it something stoopid - leave nulls for the blank case.

The advantage of this is that it works with your own structs, as long as you associate a type-converter with them, which you can do easily (with an attribute or at runtime).

public static T? Parse<T>(string value) where T : struct
{
    if (string.IsNullOrEmpty(value)) return null;
    return (T)TypeDescriptor.GetConverter(typeof(T))
        .ConvertFromInvariantString(value);
}
like image 3
Marc Gravell Avatar answered Nov 15 '22 13:11

Marc Gravell