Solved

Why generic list assignment can't be ByVal

Posted on 2011-03-07
6
599 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
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now