views:

58

answers:

1

I have a class which I use to record a value in a given interval. I.e. the value between 0 and 1 might be 0.5, 1 to 5 might be 1, and 5 to 100 might be 5. However I would like to have this information stored in a class that uses generics.

So when I try to find out the value in a certain interval I would like to be able to call on a function like this

Public Function getStepSize(ByVal value As T) As U
    For Each s As StepSizeSector(Of T, U) In _sectors
        If value >= s.from AndAlso value <= s.to Then Return s.stepSize
    Next
End Function

Where T is the type for the interval boundaries and U is the actual value for interval.

When I do this I get an error saying Operator '>=' is not defined on the types 'T' and 'T'

How can I force this comparison to take place? I will only be using types that allow comparisons, i.e. Integer, Double, Decimals, etc.

Is there are way to explicitly inform the compiler that a type has these comparison operators?

Thanks for the help, Regards.

+1  A: 

all of your types implement IComparable(Of T), so you'll need to add this constraint to your generic type definition and replace comparison operators with CompareTo() calls.

max