繁体   English   中英

为什么我在使用数组函数时会报错

[英]Why do I get an error when I use the array function

我测试了这个 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

如果只是为了简化代码,您可以使用辅助函数。

假设我们谈论的是 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM