Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So apparently because of my lack of understanding of reference types.
when I try to create a random list the original list is a duplicate of the
random one. I'm sure I will feel stupid for the answer but here is the code -
class TwoLists
{
    List<Player> _players;
    public List<Player> Players
    {
        get { return _players; }
    }
    //constructor
    public TwoLists()
    {
        _players = new List<Player>();
        CreateLists();
    }
    private void CreateLists()
    {
        for (int i = 0; i < 10; i++)
        {
            Player p = new Player("Player " + i.ToString());
            _players.Add(p);
        }
        CreateRandomList();
    }
    private void CreateRandomList()
    {
        List<Player> tempList = new List<Player>();
        tempList = _players; //  <<<<<<<<<
        Player tempPlayer = new Player();
        Random r = new Random();
        int pCount = _players.Count - 1;
        int rNum = 0;
        for (int i = 0; i <= pCount; i++)
        {
            for (int j = 0; j < 100; j++)
            {
                rNum = r.Next(0, pCount);
                tempPlayer = tempList[i];
                tempList[i] = tempList[rNum];
                tempList[rNum] = tempPlayer;
            }
        }
                // I DID NOT TELL _players TO = templist .
    }
}

So Now when I access Players it is randomized even though I have not randomized it.
so tempList is = to _players. then I work with templist and _players
duplicates it. so My question is how do I leave _players intact.
and what is going on here. obviously I should understand this by now and
so mabey Your answer will make me see what I surely should have aready understood.
and I appreciate your time. also maybey good for other beginners.
Posted
Updated 17-Jul-11 4:20am
v2

tempList = _players anything you do to tempList you do to _players, they are both referencing the same object.

You need to create a new list, then make a copy of each element.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
o1010wanabe 17-Jul-11 10:41am    
Thanks Espen .
Espen Harlinn 17-Jul-11 11:10am    
Thank you, o1010wanabe
thatraja 17-Jul-11 11:02am    
5!
Espen Harlinn 17-Jul-11 11:11am    
Than you, thatraja :)
Sergey Alexandrovich Kryukov 18-Jul-11 4:26am    
Correct, a 5. Needs a shallow clone.
--SA
Well, actually, you did.
This is not going to be too easy, without pictures and a quick check every now and then to see if your eyes are glazing over, but here goes...

You have a variable "players" which refers to a list of Player objects. Note that "players" is not the list itself, it is just a reference to it. The difference is that your list could be a set of physical cards containing names and addresses, and "players" could be a piece of paper saying "see card index set 'Names and Addresses'". You can have many pieces of paper, all referring to the same set of cards, but they are all different sheets of paper.

When you create a new list variable,
List<player> tempList = new List<player>();
You create a new sheet of paper, but this time it says "see card index set 'Temporary'" - it refer to a different set of cards, currently empty.
You then do this:
tempList = _players; //  <<<<<<<<<
All this does is use a pencil eraser on the "tempList" sheet, running out all the information, and writing in "see card index set 'Names and Addresses'". It copies the Reference, not the content.

So when you randomize the set of card referred to by the "tempList" sheet, you are also randomizing the set of cards refered to by the "players" sheet (because they both refer to the same cards).

If you want to work with a different set of cards, then you have to copy the list to your new location. Try:
List<player> tempList = new List<player>();
tempList.AddRange(_players);
Player tempPlayer = new Player();

This will create a new list all referring to the same cards (because each item in the list is not the card, it is also a reference to the card - ah, look, your eyes are glazing over).

Does that make sense? I hope it does, because this is a very important concept, and if you don't understand it you are going to cause yourself real trouble in the future!


[edit]Fixed < and >, removed spurious closing tags - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
o1010wanabe 17-Jul-11 10:38am    
I Do understand now and it has caused me trouble , thanks alot for the illistration this is important and I I get it. I mean I have studied this
and understood the concept but it doesnt click when it counts. so again
Thanks.
Espen Harlinn 17-Jul-11 10:42am    
Well - my 5, I nearly felt my "eyes glazing over" - It's a novel explanation of references :)
OriginalGriff 17-Jul-11 10:46am    
Mine were glazing over as I typed it... :laugh:
It's a whole lot easier to explain with real bits of paper on a desk!
Espen Harlinn 17-Jul-11 11:02am    
I would guess so :)
o1010wanabe 17-Jul-11 10:59am    
Thanks for AddRange() too, waycool.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900