通用类(T) - 从一系列类型VB.Net中指定

Jef*_*ern 2 .net vb.net generics

这是我正在尝试开发的代码:

  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
Run Code Online (Sandbox Code Playgroud)

它不会编译,因为我收到一个错误,'>'没有为T和T的类型定义.无论如何,我可以指定约束,保证T是一个数字类型?

这是我在用户进行更改和建议后目前所拥有的.它仍然无法正常工作.我是否必须将T的值更改为IComparable?必须有一些非常简单的东西,我搞砸了.

   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
Run Code Online (Sandbox Code Playgroud)

Mat*_*ton 7

您可以将T约束为IComparable.这样你知道curStat和maxStat都有一个CompareTo方法,你可以用它来确定一个是否大于另一个.