Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

Why generic list assignment can't be ByVal

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
Newbis
Asked:
Newbis
2 Solutions
 
wdosanjosCommented:
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
 
JoelDevCommented:
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
 
PagodNaUtakCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
KelmenCommented:
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
 
käµfm³d 👽Commented:
0
 
NewbisAuthor Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now