Code I want to work:
<Extension()> Public Function NValue(Of T)(ByVal value As Nullable(Of T), ByVal DefaultValue As T) As T Return If(value.HasValue, value.Value, DefaultValue) End Function
So basically what I want it to do is to assign a default value to an object with null values, and depending on whether it is null or not, it will give its own value or default value.
So, with a Date object, it will do this, which works, but I cannot get it to work with a common T:
<Extension()> Public Function NValue(ByVal value As Date?, ByVal DefaultValue As Date) As Date Return If(value.HasValue, value.Value, DefaultValue) End Function Dim test As Date? = Nothing Dim result = test.NValue(DateTime.Now)
Now the variable 'result' has the current DateTime.
When I try to use it with T, I get this as an error (which Visual Studio puts on T in Nullable (Of T): The type "T" must be a value type or a type argument limited to "structure" to be used with "Nullable" or a modifier with a null value of "?".
Thanks so much for any help!
Hello
FrieK source share