简体   繁体   English

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

[英]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.

相关问题 当我尝试在Google脚本中使用数组映射函数时出现语法错误。 为什么? - I get a syntax error when I try to use array map function in Google Scripts. Why? 为什么在指针数组中使用 realloc 时会出现运行时错误? - Why do I get a runtime error when I use realloc in an array of pointers? 当我在二维数组参数中使用不带括号的引用运算符时,为什么会出现错误“声明为引用数组”? - Why do I get error “declaration as array of references” when I use the reference operator without parentheses in a 2D array parameter? 当我试图在函数中返回此数组时,为什么会出现错误“数组初始值设定项必须是初始化列表”? - Why do I get the error “Array initializer must be an initializer list” when I'm trying to return this array in a function? 在Swift中创建数组时为什么会出现错误? - Why do I get an error when creating an array in Swift? 为什么在数组列表数组上使用 Arrays.fill() 时会出现意外行为? - Why do I get unexpected behaviour when I use Arrays.fill() on an array of array lists? 为什么在尝试使用file_get_contents()函数时收到PHP错误406,以及如何解决? - Why do I get a PHP error 406 when trying to use file_get_contents() function and how to solve it? 当我使用一维数组时,为什么在 C 语言中出现分段故障错误 - WHY I GET SEGEMENT FAULT ERROR IN C LANGUAGE WHEN I USE 1-D ARRAY 为什么当我打印由“主”function 交付的二维数组时出现段错误 - why i get segment error when i print 2-D array which is delivery by the "main" function 将矩阵/数组声明为全局变量时,为什么会出现错误? - Why do I get an error when I am declaring a Matrix/Array as a global variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM