[英]I get a syntax error when I try to use array map function in Google Scripts. Why?
[英]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.