Solved

Why generic list assignment can't be ByVal

Posted on 2011-03-07
6
602 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
The curse of the end user strikes again      

You’ve updated all your end user’s email signatures. Hooray! But guess what? They’re playing around with the HTML, adding stupid taglines and ruining the imagery. Find out how you can save your signatures from end users today.

 
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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

861 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now