Ho 3 funzioni simili, che solo cambiano in base al tipo numerico:Creazione di un metodo generico per la conversione di stringhe in numeri nullable
<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
stavo cercando di fare una generica funzione di questo, ma non poteva fallo. Come posso creare una funzione generica che prenderà una T specifica e la renderà un Nullable (di T)?
È troppo male non esiste un metodo Convert.TryChangeType, che salverebbe dover mangiare un'eccezione quando la stringa di input non è valida. – jjxtra