通用类(T) - 从类型的范围VB.Net指定 [英] Generic Classes (T) - Specifying from a Range of Types VB.Net

查看:170
本文介绍了通用类(T) - 从类型的范围VB.Net指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在code我试图开发:

 公共结构统计(的T)
        昏暗maxStat为T
        昏暗curStat为T

        公共子新(BYVAL P值作为T)
            maxStat = P值
            curStat = P值
        结束小组

        公共属性Level()为t
            得到
                返回curStat
            最终获取
            设置(BYVAL值作为T)
                curStat =价值
                如果curStat> maxStat然后curStat = maxStat
            结束设定
        高端物业
    末端结构
 

这不能编译,因为我得到了'>'没有被定义为类型T和T的错误反正我有可以指定机制保障T是一个数值类型的限制?

这是我目前已经从用户的更改和建议后。它仍然没有工作。我一定要改变T的值,所有的人都为IComparable的?必须有很简单的东西是我搞砸了。

 公共结构统计(对T作为IComparable的(对T))
        昏暗maxStat为T
        昏暗curStat为T

        公共子新(BYVAL P值作为T)
            maxStat = P值
            curStat = P值
        结束小组

        公共属性统计()为t
            得到
                返回curStat
            最终获取
            设置(BYVAL值作为T)
                如果值GT; maxStat然后

                结束如果
            结束设定
        高端物业
    末端结构
 

解决方案

在这里,你去...这应该工作。

 公共结构统计(T为{} IComparable的)
   昏暗maxStat为T
   昏暗curStat为T

   公共子新(BYVAL P值作为T)
      maxStat = P值
      curStat = P值
   结束小组

   公共属性Level()为t
      得到
            返回curStat
      最终获取
      设置(BYVAL值作为T)
            curStat =价值
            如果curStat.CompareTo(maxStat)> 0然后curStat = maxStat
      结束设定
   高端物业
末端结构
 

另外,你提到的限制,以数字,但我不认为你可以约束到这一点。但是,您可以限制到只基本类型(在栈上),而不是让任何物体(堆)通过执行此操作:公共结构统计(T为{结构,IComparable的})

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

解决方案

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}).

这篇关于通用类(T) - 从类型的范围VB.Net指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆