VB6与VB.NET中将数组作为参数传递给过程的区别

3

这里有一个在vb6中的程序,像示例一样正常工作:

' Check_UnCheck

' 选中一个复选框数组,取消选中另一个复选框数组

' 使用示例:

CheckBox.Check_UnCheck Array(chkCheck3, chkCheck5), Array(chkCheck1, chkCheck4)


Public Sub Check_UnCheck(ByRef CheckArray As Variant, ByRef UnCheckArray As Variant)

    Dim i As Integer
    Dim conControl As Control

    For i = LBound(CheckArray) To UBound(CheckArray)
        Set conControl = CheckArray(i)
        conControl.Value = 1
    Next

    For i = LBound(UnCheckArray) To UBound(UnCheckArray)
        Set conControl = UnCheckArray(i)
        conControl.Value = 0
    Next

End Sub

以上过程在VB.NET中的等效方式是什么?MSDN文档指出:

  • 我们不能在一个过程中使用超过一个参数数组,而且它必须是过程定义中的最后一个参数。

3
一个方法可以有多个参数数组。你所提到的限制是针对ParamArray的,不是同一件事情。 - Ňɏssa Pøngjǣrdenlarp
2个回答

2
尝试以下代码。详细说明请查看注释。
'DECLARE YOUR ARRAYS.
Dim array1 = New CheckBox() {CheckBox3, CheckBox5}
Dim array2 = New CheckBox() {CheckBox1, CheckBox4}

'CALL CHECK AND UNCHECK FUNCTION.
Check_UnCheck(array1, array2)


'YOUR FUNCTION DEFINITION.
Public Sub Check_UnCheck(ByRef CheckArray As CheckBox(), ByRef UnCheckArray As CheckBox())

    'LOOP FIRST ARRAY AND CHECK THEM.
    For index = 0 To CheckArray.GetUpperBound(0)
        CheckArray(index).Checked = True
    Next

    'LOOP SECOND ARRAY AND UNCHECK THEM.
    For index = 0 To UnCheckArray.GetUpperBound(0)
        UnCheckArray(index).Checked = False
    Next

End Sub

这个方法不需要使用ByRef参数。 - jmoreno
@jmoreno 如果有人对这门语言不熟悉,明确地指出ByRef即使在技术上是多余的,可能会更清晰。 - StayOnTarget
@DaveInCaz:这不是多余,而是谎言。ByRef表示在调用方法中用作参数的变量可能会被更改。但实际上并没有发生这种情况。 - jmoreno
@jmoreno,就像你所说的那样,这并不是必需的,但我故意留下它,因为所涉及的函数签名中有“byref”。我的假设是OP可能需要它用于其他事情,而OP在问题中没有发布。 - Pavan Chandaka
非常好,简单易用,运行良好。谢谢。它将是 >> Check_UnCheck({CheckBox3, CheckBox5}, {CheckBox1, CheckBox4})。 - Max

1
首先,您混淆了“参数数组”和控件数组,它们不是同一回事。参数数组是指一个方法接受可变数量的参数(类型相同),编译器将其打包成一个数组并将其传递给该方法。为了实现这一点,该方法必须使用关键字ParamArray
VB.net没有像vb6那样的控件数组,但这不是您示例使用的方式。您的示例正在使用简单的控件数组。另外,您的示例正在使用ByRef,在VB6中是默认值,但在这种情况下对于VB6和VB.net都是不必要的。鉴于在VB.net中它的用法不再是默认值,不必要地使用它会产生误导。
您正在传递两个数组,第二个数组可以是ParamArray,但这样做没有什么意义。
要使代码正常工作的基本和最小更改很简单,只需将“Variant”更改为“CheckBox()”。但我不建议这样做。还有几个次要的更改,可以使代码更加灵活和易读。
Public Sub Check_UnCheck(CheckArray As IEnumerable(Of CheckBox), 
                         UnCheckArray As IEnumerable(Of CheckBox)) 
    ' Here I have replaced the Variant, which is not supported in
    ' .net, with the generic IEnumerable of checkbox.  I used an
    ' Ienumerable, instead of an array because it will allow making 
    ' just a minor change to the call site.

    ' here I have eliminated the index variable, and moved the  
    ' declaration of the conControl variable into the for each.  
    ' Option Infer On statically types the variable as a checkbox
    For Each conControl In CheckArray
        ' Checkbox controls do not have a value property.
        ' 1 is not true, true is true.
        conControl.Checked = True
    Next

    For Each conControl in UnCheckArray
        conControl.Checked = False
    Next

End Sub

这样就可以调用它: Check_UnCheck({chkCheck3, chkCheck}, {chkCheck1, chkCheck4})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接