Passing generic lists by val
Posted on 2011-02-14
Is there a way to pass a generic list (List of T) by val instead of By Ref?
I created a test app that had this:
Public Class Vendor
private m_Id as integer
public property ID as integer
Set(ByVal value As integer)
No I created another class
Public class Test
private mList as new list(of Vendor)
public sub New(VendorList as list(of Vendor))
mList = VendorList
No in main form I click a button and do this:
Dim myList As New List(Of Vendor)
For i As Integer = 0 To 10
Dim v As New Vendor
Dim t As New Test(myList)
MsgBox("List Count: " & CStr(myList.Count))
The results show that the clear function in the Test class cleared the passed in list even though it was passed by val. Back in form1 the msgbox shows the count as 0.
Is there a way I can pass the list to Test without it actually clearing the original list?