为什么在使用数组函数时出现错误

问题描述 投票:0回答:1

我测试了此Autocad VBA例程。有效。没问题。

Sub Add_Line_1()
    Dim n1(2) As Double, n2(2) As Double
    Dim r As AcadLine
    n1(0) = 100
    n1(1) = 150

    n2(0) = 220
    n2(1) = 230
    Set r = ThisDrawing.ModelSpace.AddLine(n1, n2)
End Sub

但是。我想使用Array功能。没用发生错误。

运行时错误5:无效的过程调用或参数

Sub Add_Line_2()
    Dim n1 As Variant, n2 As Variant
    Dim r As AcadLine
    n1 = Array(100#, 150#)
    n2 = Array(220#, 230#)

    ' ERROR LINE.
    Set r = ThisDrawing.ModelSpace.AddLine(n1, n2)
End Sub

如何在此代码中使用Array功能?

编辑:我尝试了此代码,但再次出现错误

编译错误。无法分配给数组

Sub Add_Line_3()
    Dim n1(2) As Double, n2(2) As Double
    Dim r As AcadLine
    n1 = Array(100#, 150#, 0#) 'ERROR LINE
    n2 = Array(220#, 230#, 0#)

    Set r = ThisDrawing.ModelSpace.AddLine(n1, n2)
End Sub
arrays vba autocad
1个回答
0
投票

如果只是为了简化代码,则可以使用帮助器功能。

假设我们正在谈论2D / 3D空间中的点,我们可以定义:

Function Point(x As Double, y As Double, Optional z As Double = 0) As Double()
    ReDim temp(2) As Double
    temp(0) = x
    temp(1) = y
    temp(2) = z
    Point = temp
End Function

和使用

ThisDrawing.ModelSpace.AddLine(Point(100, 150), Point(220, 230))
© www.soinside.com 2019 - 2024. All rights reserved.