views:

44

answers:

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
+7  A: 

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
Example: Statistic(Of t as IComparable(Of T))
Jonathan Allen
Ok. I've tried this and it still doesn't seem to be working.I'll update the code.
Jeffrey Kern
Thanks for your help! I +1'd it :)
Jeffrey Kern
I gave you one too just because we tag teamed this one. Didn't mean to swoop in on you.
mattmc3
Wow. I just realized that ">" does not mean "CompareTo". I'm a moron haha. Wow..... just wow.
Jeffrey Kern
@mattmc3 you deserved the accepted answer 'coz you provided code. I can't code my way out of a wet paper bag in VB so I didn't risk including a code sample. :)
Matt Hamilton
+2  A: 

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
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
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
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
@mattmc 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