Nullables in type parameters?
-
Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:
public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : IComparable<T>
I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT) -
Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:
public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : IComparable<T>
I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT)I think you need to specify INullable as well as IComparable.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:
public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : IComparable<T>
I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT)Dominik Reichl wrote:
I think it's pretty obvious what I'm trying to do
Not really - I think your compiler message is hiding another. You have declared that T is a IComparibale<T>, which is a reference type which is already nullable (nullables are only useful for value types). But declaring that T is an IComparible<T> is a circular reference. Unless I've missed something, T must therefore be an ICompariblae<IComparible<IComparible<Icomparible<....>>>>
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
Dominik Reichl wrote:
I think it's pretty obvious what I'm trying to do
Not really - I think your compiler message is hiding another. You have declared that T is a IComparibale<T>, which is a reference type which is already nullable (nullables are only useful for value types). But declaring that T is an IComparible<T> is a circular reference. Unless I've missed something, T must therefore be an ICompariblae<IComparible<IComparible<Icomparible<....>>>>
Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
Ok, so I modified it to non-generic comparable:
private static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : IComparable
Anyway, still exactly the same compiler error is thrown... :(
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT) -
I think you need to specify INullable as well as IComparable.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
INullable
seems to be deeply hidden in the SQL namespace... Also, this interface only adds aIsNull
property (but I also needHasValue
andValue
of "normal"Nullable
s). Are you sure I need this interface??
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT) -
Ok, so I modified it to non-generic comparable:
private static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : IComparable
Anyway, still exactly the same compiler error is thrown... :(
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT)The point is that you can remove the ? in T?, because IComparable means you can only pass classes, which are all nullable by default.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hello! I'm trying to write a function that checks whether a given value is in a range defined by a minimum and a maximum. I tried this:
public static bool ValidateNumber<T>(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : IComparable<T>
I think it's pretty obvious what I'm trying to do ;) The minimum and the maximum should be optional, therefore nullables. Anyway, the declaration above gives me a compiler error CS0453 ("Type T must not allow NULL values, if it is used as T parameter in the generic type or in the generic method System.Nullable<T>"). How does the correct declaration look like in this case? Best regards Dominik
_outp(0x64, 0xAD);
and__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? ;) (doesn't work on NT)Remember that nullable types can only take value types as the generic argument (as value types cannot be null). So in your declaration you just need to specify that T is a value type: public static bool ValidateNumber(T tNum, T? tMinimum, bool bMinInclusive, T? tMaximum, bool bMaxInclusive) where T : struct, IComparable Hope this helps. :)