[英]Why do I get an error when I use the array function
I tested this Autocad VBA routine.我测试了这个 Autocad VBA 例程。 It worked.有效。 No problem.没问题。
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
But.但。 I want to use Array
function.我想使用Array
函数。 It not worked.它没有用。 An error occurred.发生错误。
Run-time Error 5: Invalid procedure call or argument运行时错误 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
How to use Array
function in this code?如何在这段代码中使用Array
函数?
EDIT: I tried this code but again error编辑:我试过这段代码,但再次出错
Compile error.编译错误。 Can't assign to 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
If it's just about streamlining the code, you can use a helper function.如果只是为了简化代码,您可以使用辅助函数。
Assuming that we are talking about a point in 2D/3D space, we could define:假设我们谈论的是 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
and use并使用
ThisDrawing.ModelSpace.AddLine(Point(100, 150), Point(220, 230))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.