2

This is the code I'm trying to develop:

  Public Structure Statistic(Of t)
        Dim maxStat As t
        Dim curStat As t

        Public Sub New(ByVal pValue As t)
            maxStat = pValue
            curStat = pValue
        End Sub

        Public Property Level() As t
            Get
                Return curStat
            End Get
            Set(ByVal value As t)
                curStat = value
                If curStat > maxStat Then curStat = maxStat
            End Set
        End Property
    End Structure

It won't compile because I'm getting an error that '>' is not defined for types of T and T. Is there anyway I can specify constraints that guarentee that T is of a numeric type?

This is what I currently have after changes and suggestions from the users. It's still not working. Do I have to change the values of T for all of them to be IComparable? There must be something really simple that I'm screwing up.

   Public Structure Statistic(Of T As IComparable(Of T))
        Dim maxStat As T
        Dim curStat As T

        Public Sub New(ByVal pValue As T)
            maxStat = pValue
            curStat = pValue
        End Sub

        Public Property Statistic() As T
            Get
                Return curStat
            End Get
            Set(ByVal value As T)
                If value > maxStat Then

                End If
            End Set
        End Property
    End Structure
Jeffrey Kern
  • 2,024
  • 20
  • 40

2 Answers2

7

You could constrain T to IComparable. That way you know that curStat and maxStat both have a CompareTo method you can use to determine if one is greater than the other.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
4

Here you go... this should work.

Public Structure Statistic(Of t As {IComparable})
   Dim maxStat As t
   Dim curStat As t

   Public Sub New(ByVal pValue As t)
      maxStat = pValue
      curStat = pValue
   End Sub

   Public Property Level() As t
      Get
            Return curStat
      End Get
      Set(ByVal value As t)
            curStat = value
            If curStat.CompareTo(maxStat) > 0 Then curStat = maxStat
      End Set
   End Property
End Structure

Also, you mentioned constraining to numerics, but I don't think you can constrain to just that. You can however constrain to just primitive types (on the stack) and not allow objects (on the heap) by doing this: Public Structure Statistic(Of t As {Structure, IComparable}).

mattmc3
  • 17,595
  • 7
  • 83
  • 103
  • And this works. Stupid stupid horizontal-pointed arrows. Or, alligators that always ate the grapes that had the most in the bunch. XD – Jeffrey Kern Jul 29 '10 at 00:05
  • I loved those sorts of tricks from elementary school. They always stick with you. The other one that helps my kids is that your left hand makes a correct "L" (pointer+thumb). Mnemonics like that made learning stuff so much easier as a kid... – mattmc3 Jul 29 '10 at 00:13
  • Using structures works great as well. I've never used Generics because I've always found it easier - or less thought-provoking - to just hardcode types and use lots-of-inheritance. – Jeffrey Kern Jul 29 '10 at 00:13
  • For the longest time I had issues figuring out 'greater than' or 'less than'. Specifically in emementary school, it just confused me. I always asked for 'points to the left or right' haha. – Jeffrey Kern Jul 29 '10 at 00:15