Solved

Why generic list assignment can't be ByVal

Posted on 2011-03-07
6
607 Views
Last Modified: 2012-05-11
I'm just curious why in the following function (for System.Collections.Generic.List), the ByVal keyword doesn't appear to be followed:

    Private Function GetListByVal(ByVal Input As List(Of Integer)) As List(Of Integer)
        Return Input
    End Function

If I set another List object equal to this function and then sort it, the original list that I fed in also gets sorted.  Therefore, it appears that "ByVal" is being ignored.

I understand that if I set one List object equal to another, then it will really be just a reference to the first one, so that sorting on one variable affects both.  But even if I try to force .NET to duplicate the list, it still apparently maintains only one list with two references to it.
0
Comment
Question by:Newbis
6 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35063298
Input in your case is only a reference to the List(Of Integer) object not the actual object.  So, passing it by reference or by value will always point to the same List(Of Integer) object, thus when you make changes to it in the Function you change the actual list.

You have to manually create another List(Of Integer) to achieve the ByVal behavior you want.  It would be something like this:

    Private Function GetListByVal(ByVal Input As List(Of Integer)) As List(Of Integer)
        Input = Input.Clone()
        Return Input
    End Function

I hope this helps.
0
 

Expert Comment

by:JoelDev
ID: 35063583
As you mentioned; you're just creating a pointer to the original. I think using Input.ToList() will create a copy of your original list.
0
 
LVL 8

Assisted Solution

by:PagodNaUtak
PagodNaUtak earned 100 total points
ID: 35063588
Because List is a reference type, Byval and ByRef works only in value types. So, in the end you only have one list with two references to it.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 5

Accepted Solution

by:
Kelmen earned 400 total points
ID: 35063616
in c#, you can clone/create a new list base on another in this way:

List<x> newXs = new List<x>(oldXList)

look up the list<T> constructor
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35089149
0
 

Author Closing Comment

by:Newbis
ID: 35091096
The c-sharp example worked.

In VB.NET, the corresponding syntax (for a list of integers) is like this:
Output = New List(Of Integer)(Input)

I did not find any "Clone" method for List, although other objects do have a Clone method.

I awarded some points to the comment that it has to do with what are called reference vs. value types, as that's useful information.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extra security implementation for 2017 9 53
List<PaisEntity> - show some 1 24
VB.NET 2008 Winforms Signing 13 20
Disk Quota Windows 2012 R2 5 53
This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question