ByVal在VB.NET中无法正常工作

ByVal在VB.NET中无法正常工作,vb.net,pass-by-value,Vb.net,Pass By Value,我在VB.NET中有这样一个代码: ' This code will sort array data Public Sub SelectionSort(ByVal array as ArrayList) For i as Integer = 0 To array.Count -1 Dim index = GetIndexMinData(array, i) Dim temp = array(i) array(i) = array(index)

我在VB.NET中有这样一个代码:

' This code will sort array data
Public Sub SelectionSort(ByVal array as ArrayList)
   For i as Integer = 0 To array.Count -1
      Dim index = GetIndexMinData(array, i)
      Dim temp = array(i)
      array(i) = array(index)
      array(index) = temp
   Next
End Sub

Public Function GetIndexMinData(ByVal array As ArrayList, ByVal start As Integer) As Integer
    Dim index As Integer
    Dim check As Integer = maxVal
    For i As Integer = start To Array.Count - 1
        If array(i) <= check Then
            index = i
            check = array(i)
        End If
    Next
    Return index
End Function

' This code will sort array data
Public Sub SelectionSortNewList(ByVal array As ArrayList)
    Dim temp As New ArrayList

    ' Process selection and sort new list
    For i As Integer = 0 To array.Count - 1
        Dim index = GetIndexMinData(array, 0)
        temp.Add(array(index))
        array.RemoveAt(index)
    Next
End Sub

Private Sub btnProcess_Click(sender As System.Object, e As System.EventArgs) Handles btnProcess.Click
    Dim data as new ArrayList
    data.Add(3)
    data.Add(5)
    data.Add(1)
    SelectionSort(data)
    SelectionSortNewList(data)
End Sub
”此代码将对数组数据进行排序
公共子选择排序(ByVal数组作为ArrayList)
对于数组,i为整数=0。计数-1
Dim index=GetIndexMinData(数组,i)
Dim temp=阵列(i)
数组(i)=数组(索引)
数组(索引)=温度
下一个
端接头
公共函数GetIndexMinData(ByVal数组作为ArrayList,ByVal开始作为Integer)作为Integer
将索引设置为整数
尺寸检查为整数=最大值
对于i As Integer=start To Array.Count-1

如果数组(i)即使在对象上使用
ByVal
,也可以修改对象的
属性

不能修改对象的实例,但不能修改其属性

例如:

Public Class Cars

    Private _Make As String
    Public Property Make() As String
        Get
            Return _Make
        End Get
        Set(ByVal value As String)
            _Make = value
        End Set
    End Property

End Class
如果我以拜瓦尔的身份通过了这门课

Private sub Test(ByVal MyCar as Car)

MyCar.Make = "BMW"

End Sub
当您指向同一对象并修改其属性时,该属性的值将发生变化。

为什么不使用该方法?其次,我选择了一个(在本例中为T=Integer)而不是一个ArrayList,因为它在编译时是类型安全的。最后,我建议研究值类型和引用类型之间的区别,了解它们的区别是非常重要的。