【问题标题】:value of type integer cannot be converted to object because integer in not reference type整数类型的值无法转换为对象,因为整数不是引用类型
【发布时间】:2016-04-09 18:53:04
【问题描述】:
Module Module1
Sub main()
    Dim pl(), pll() As Integer
    Dim a, b As Integer
    ReDim pl(0)
    ReDim pll(0)
    Do
        a = InputBox("insert number:")
        If a <> 0 Then
            b = b + 1
            ReDim Preserve pl(b)
            pl(b) = a
        End If
    Loop Until a = 0
    pll = **se(pl)**

End Sub
Function se(pol()) As Integer()
    Dim r, t, w, m As Integer
    Dim fix() As Integer
    ReDim fix(0)
    r = UBound(pol)
    w = 2
    For t = 1 To r
        For m = 1 To r
            If w <= r Then
                If pol(w) < pol(t) Then
                    ReDim Preserve fix(t)
                    fix(t) = pol(w)
                End If
            End If
            w = w + 1
        Next
    Next

    se = fix
End Function

结束模块

嗨,我创建了这个函数(不知道它是否工作)se(pl),它接受数字数组并返回该数组,但按升序排列。但是当我想将该函数分配给数组时 - pll=se(pl) 它给了我这个错误==>“整数类型的值不能转换为对象,因为整数不是引用类型”

对不起,我是菜鸟,有人可以帮忙吗?

【问题讨论】:

    标签: arrays vba visual-studio function


    【解决方案1】:

    我调整了您的代码以使其正常工作。然而,我尝试尽可能地坚持原始代码,以便您更容易地从这个解决方案中学习:

    Option Base 0
    Option Explicit
    
    Sub main()
    
    Dim pl() As Integer
    Dim a As Integer, b As Integer
    ReDim pl(0)
    
    Do
        a = InputBox("insert number:")
        If IsNumeric(a) And a <> 0 Then
            b = b + 1
            ReDim Preserve pl(b)
            pl(b) = a
        End If
    Loop Until a = vbNullString
    
    Debug.Print "Unsorted:"
    For a = LBound(pl) To UBound(pl)
        Debug.Print pl(a)
    Next a
    
    se intArray:=pl
    
    Debug.Print "Sorted:"
    For a = LBound(pl) To UBound(pl)
        Debug.Print pl(a)
    Next a
    
    End Sub
    Function se(ByRef intArray() As Integer)
    
    Dim t As Integer, w As Integer, m As Integer
    
    For t = LBound(intArray) To UBound(intArray)
        For m = LBound(intArray) To UBound(intArray)
            If intArray(t) < intArray(m) Then
                w = intArray(t)
                intArray(t) = intArray(m)
                intArray(m) = w
            End If
        Next m
    Next t
    
    End Function
    

    一些重要说明:

    (1) 如果你想Dim 一行中的多个变量,那么你必须为每个变量重复DataType。所以,它是Dim a as Integer, b as Integer 而不是Dim a, b as Integer。在后两种情况下,a 将是DataType 的变体(并且不是,可能是预期的Integer)。

    (2) 要将 VBA 中的 arrays 从过程传递给函数,您必须将其传递给 ByRef。因此,无需创建第二个或第三个array(例如pll()fix())。

    (3) 有一个 VBA 命令Fix。因此,您不能将其用于变量。

    如果以上内容有帮助,或者您需要更多背景知识或稍作调整,请告诉我。

    【讨论】:

      【解决方案2】:

      您同时标记了“VBA”和“Visual Studio”,但以下内容仅适用于 VBA

      至于您的代码,我仅通过将fix 替换为fixed 使其工作(意味着它运行到最后没有错误),因为我的Excel VBA“编译器”由于以下原因而引发“语法错误”消息“FIX()”VBA 函数的存在。

      所以,在调用替换 Function se 之后,它返回了一个没有问题的整数数组。

      但它没有按照您所说的去做,即:“按升序返回传递的数组”。

      此外,您的变量声明和输入过程都允许传递字符串。

      所以这里按照我的建议让您的代码使用更强大的输入过程执行您所说的需要(“以升序返回传递的数组”)

      Option Explicit
      
      Sub main()
          Dim pl() As Integer, pll() As Integer
      
          pl = TryConvertToInt(Split(InputBox("insert numbers (separated by space):")))
      
          pll = se(pl)
      
      End Sub
      
      
      Function TryConvertToInt(arr As Variant) As Integer()
      Dim i As Long, n As Long
      ReDim myint(1 To UBound(arr) - LBound(arr) + 1) As Integer
      
      For i = LBound(arr) To UBound(arr)
          If IsNumeric(arr(i)) And arr(i) <> 0 Then
              n = n + 1
              myint(n) = CInt(arr(i))
          End If
      Next i
      ReDim Preserve myint(1 To n) As Integer
      
      TryConvertToInt = myint
      End Function
      
      
      Function se(pol() As Integer) As Integer()
      
      'adapted from https://support.microsoft.com/en-us/kb/133135
      
          Dim Temp As Integer
          Dim i As Integer
          Dim NoExchanges As Boolean
      
          Dim fixed() As Integer
          ReDim fixed(1 To UBound(pol) - LBound(pol) + 1)
      
          fixed = pol
          ' Loop until no more "exchanges" are made.
          Do
              NoExchanges = True
      
              ' Loop through each element in the array.
      '        For i = 1 To UBound(pol) - 1
              For i = LBound(fixed) To UBound(fixed) - 1
      
                  ' If the element is greater than the element following it, exchange the two elements.
                  If fixed(i) > fixed(i + 1) Then
                      NoExchanges = False
                      Temp = fixed(i)
                      fixed(i) = fixed(i + 1)
                      fixed(i + 1) = Temp
                  End If
              Next i
          Loop While Not (NoExchanges)
      
          se = fixed
      
      End Function
      

      如你所见:

      • main sub 现在非常简单,简化为三个语句

        • 第一个是变量声明一个
        • 第二个语句嵌套了三个调用:

          • 调用InpuBox 函数,要求用户输入所有需要的数字,以空格分隔(并且不需要零来关闭序列)
          • 调用Split 函数,使其将输入字符串解析为由空格分隔的输入字符串组成的字符串数组
          • TryConvertToInt 函数的调用负责分析字符串数组并将其转换为整数数组,只允许零以外的数字
        • 第三条语句调用se函数并将其返回的整数数组放入pll整数数组

      • se 函数具有派生自https://support.microsoft.com/en-us/kb/133135 的排序算法,仅适用于处理整数数组。

      有了这样的结构,您现在可以更有效地进行编码,因为您可以通过调用特定的子和/来专注于 main 子做“主要”工作(做这个,做那个,...)或函数。

      这种离开的“非主”作用于特定的子或函数,仅用于特定目的:例如,您可能希望自定义 TryConvertToInt 函数以获得更详细的过滤操作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-25
        • 2012-12-20
        • 2021-01-26
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 2012-10-21
        相关资源
        最近更新 更多