经典 asp:通过引用调用函数不适用于数组

发布于 2024-11-02 19:43:34 字数 865 浏览 7 评论 0原文

我有一个数组,我通过引用传递给函数以对其进行排序。然而,似乎数组是通过val传递的。任何人都可以解决什么问题吗? (也接受排序解决方法)


1) 下面的脚本通过引用将数组传递给排序函数。

2) Sort 函数输出排序后的数组值。

3) 脚本输出排序后的数组值。然而它们没有排序。


脚本输出:

300,200,100,,

100,200,300,


'declare variables
mitta(1) = 1
mitta(2) = 2
mitta(3) = 3

sort(mitta)  ' see the function below

' show variables
For i = 1 To 3
    response.write mitta(i) & ","
next

' sort function
function sort(byref a)
    dim num,i,j,temp
    num = ubound(a)+1
    For i = 0 To num - 1
       For j = i + 1 To num - 1
          If a(i) < a(j) Then
             temp = a(i)
             a(i) = a(j)
             a(j) = temp
          End If
       Next
    Next

    ' show sorted variables
    For i = 0 To num - 1
        response.write a(i) & ","
        a(i) = 0
    next
end function

I have an array witch I pass to a function by reference to sort it. However, seems like the array is passed byval. Can anyone solve what's the problem? (Also sort workarounds accepted)


1) The script below passes an array by-reference to the sort function.

2) Sort function outputs the sorted array values.

3) The script outputs the sorted array values. However they are not sorted.


The script outputs:

300,200,100,,

100,200,300,


'declare variables
mitta(1) = 1
mitta(2) = 2
mitta(3) = 3

sort(mitta)  ' see the function below

' show variables
For i = 1 To 3
    response.write mitta(i) & ","
next

' sort function
function sort(byref a)
    dim num,i,j,temp
    num = ubound(a)+1
    For i = 0 To num - 1
       For j = i + 1 To num - 1
          If a(i) < a(j) Then
             temp = a(i)
             a(i) = a(j)
             a(j) = temp
          End If
       Next
    Next

    ' show sorted variables
    For i = 0 To num - 1
        response.write a(i) & ","
        a(i) = 0
    next
end function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2024-11-09 19:43:34

通过在函数调用 sort(mitta) 中将 mitta 括在括号中,您实际上是按值传递它,尽管有函数声明。来自 http://blogs.msdn.com/b/ericlippert/archive/2003 /09/15/52996.aspx

规则是

3.1) 函数调用的参数列表,并赋值给
返回值必须被包围
括号:结果 = MyFunc(MyArg)
3.2) 子程序调用(或没有参数的函数调用)的参数列表
赋值)使用 Call 关键字
必须用括号括起来:调用
MySub(MyArg)
3.3) 如果 3.1 和 3.2 不适用,则列表不得被包围
括号。

最后还有 byref 规则:
当参数通过引用传递时
可能,但如果有“额外”
将变量括起来,然后
变量通过val而不是byref传递。

现在应该清楚为什么了
语句 MySub(MyArg) 是合法的,但是
MyOtherSub(MyArg1, MyArg2) 不是。这
第一种情况似乎是一个子程序
在参数周围使用括号进行调用
列表,但这会违反规则 3.3。
那为什么它是合法的呢?事实上它是一个
没有括号的子例程调用
arg 列表,但用括号括起来
第一个参数! 这通过了
按值论证。第二种情况是
明显违反规则 3.3,并且
没有办法让它合法化,所以
我们给出了一个错误。

另请参阅 MSDN 参考以了解 ByRef 和 ByVal 参数。相反,您应该使用以下方式调用 sort

sort mitta

或:

Call sort(mitta)

By wrapping mitta in parentheses in the function call sort(mitta), you're actually passing it by value, despite the function declaration. From http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx:

The rules are

3.1) An argument list for a function call with an assignment to the
returned value must be surrounded by
parens: Result = MyFunc(MyArg)
3.2) An argument list for a subroutine call (or a function call with no
assignment) that uses the Call keyword
must be surrounded by parens: Call
MySub(MyArg)
3.3) If 3.1 and 3.2 do not apply then the list must NOT be surrounded by
parens.

And finally there is the byref rule:
arguments are passed byref when
possible but if there are “extra”
parens around a variable then the
variable is passed byval, not byref.

Now it should be clear why the
statement MySub(MyArg) is legal but
MyOtherSub(MyArg1, MyArg2) is not. The
first case appears to be a subroutine
call with parens around the argument
list, but that would violate rule 3.3.
Then why is it legal? In fact it is a
subroutine call with no parens around
the arg list, but parens around the
first argument!
This passes the
argument by value. The second case is
a clear violation of rule 3.3, and
there is no way to make it legal, so
we give an error.

See also the MSDN reference for ByRef and ByVal Parameters. Instead, you should call sort either with:

sort mitta

or:

Call sort(mitta)
婴鹅 2024-11-09 19:43:34
call sort(mitta)

就是这样,只需添加关键字call即可。完整参考可在此处获取

顺便说一句,您的代码有问题。数组是从 0 开始的。

call sort(mitta)

That's it, just add the keyword call. Complete reference is available here.

BTW, your code has problems. The arrays are 0 based.

百思不得你姐 2024-11-09 19:43:34

当您将对象作为参数传递时,您将指针传递给对象,而不是对象本身(这适用于我所知道的所有语言)。因此,传递 ByVal 还是 ByRef 并不重要,因为根据定义,您始终传递一个 指针(对对象的引用)

When you pass an object as parameter, you are passing a pointer to the object, not the object itself (this apply to all languages I know). So it does not matter if you pass it ByVal or ByRef, because by definition you are always passing a pointer (a reference to the object)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文