Solved

Why generic list assignment can't be ByVal

Posted on 2011-03-07
6
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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