简体   繁体   中英

Why do I get an error when I use the array function

I tested this Autocad VBA routine. 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. It not worked. An error occurred.

Run-time Error 5: Invalid procedure call or argument

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?

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:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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