有一个文本框,根据定义的type和size判断此文本框中输入的数字是否越界。
具体如下
type为"BIN-1",size = 1时,有效范围是0~255
type为"BIN-1",size = 2时,有效范围是0~65535
type为"BIN-1",size = 4时,有效范围是0~4294967295type为"BIN-2",size = 1时,有效范围是-128~127
type为"BIN-2",size = 2时,有效范围是-32768~32767
type为"BIN-2",size = 4时,有效范围是-2147483648~2147483647请指教判断方法。谢谢。

解决方案 »

  1.   

    if type 为"BIN-1" then 
       case size = 1
       case size = 2
       case size = 3if type 为"BIN-1" then 
       case size = 1
       case size = 2
       case size = 3
      

  2.   

    第二个 if 应为 type 为"BIN-2"
      

  3.   

    Public Function IsBetween(ByVal ptype As String, ByVal psize As Integer, ByVal pvalue As Long) As Boolean
        Dim Min As Long
        Dim Max As Long
        Dim power As Integer          ' 幂
        On Error GoTo Errhandle:
        If Len(Trim(ptype) = "5") Then
            GoTo Errhandle
        End If
        If Right(Trim(ptype), 1) = "1" Then
            power = psize * 8
            Min = 0
        ElseIf Right(Trim(ptype), 1) = "2" Then
            power = psize * 8 - 1
            Min = 2 ^ power
        Else
            GoTo Errhandle
        End If
        Max = 2 ^ power - 1
        If pvalue >= Min And pvalue <= Max Then
            IsBetween = True
        Else
            IsBetween = False
        End If
    Errhandle:
        IsBetween = False
    End Function
      

  4.   

    谢谢wwqna(york)。原来VB是有次方运算的,以前一直不知道 :(
    谢谢。