Click here to Skip to main content
15,912,977 members
Home / Discussions / C#
   

C#

 
AnswerRe: Proper way to sort a List<> Custom objects Pin
Luc Pattyn30-Aug-09 11:13
sitebuilderLuc Pattyn30-Aug-09 11:13 
AnswerRe: Proper way to sort a List<> Custom objects Pin
Luc Pattyn30-Aug-09 12:06
sitebuilderLuc Pattyn30-Aug-09 12:06 
GeneralRe: Proper way to sort a List<> Custom objects Pin
Dwayner7930-Aug-09 15:41
Dwayner7930-Aug-09 15:41 
QuestionC# List problem Pin
Charlesh330-Aug-09 9:50
Charlesh330-Aug-09 9:50 
AnswerRe: C# List problem Pin
Luc Pattyn30-Aug-09 10:25
sitebuilderLuc Pattyn30-Aug-09 10:25 
GeneralRe: C# List problem Pin
Charlesh330-Aug-09 12:37
Charlesh330-Aug-09 12:37 
GeneralRe: C# List problem Pin
Luc Pattyn30-Aug-09 12:44
sitebuilderLuc Pattyn30-Aug-09 12:44 
AnswerRe: C# List problem Pin
OriginalGriff30-Aug-09 21:27
mveOriginalGriff30-Aug-09 21:27 
Your problem is quite simple, but may take some thinking about. The reason you get the problem is because of this:
if (MemoryBank[j].bUsed == 1)
   {
   MemoryBank[i] = MemoryBank[j];
   MemoryBank[j].bUsed = 0;
   MemoryBank[i].bUsed = 1;
   }


If you try the following smaller code fragment, you will get the same result:
List<cTemp> ls = new List<cTemp>(10);
int i;
cTemp ct;
for (i = 0; i < 10; i++)
    {
    ct = new cTemp(i.ToString(), i);
    ls.Add(ct);
    }
ls[5] = ls[6];
ls[6].s = "Hello";
ls[5].i = 42;
for (i = 1; i < 10; i++)
    {
    Console.WriteLine("{0}:{1}", ls[i].s, ls[i].i);
    }


class cTemp
    {
    public string s;
    public int i;
    public cTemp(string ss, int ii)
        {
        s = ss;
        ii = i;
        }
    }

Why? Because the elements of the List are references rather than the specific variables. By assigning a different reference via the line
MemoryBank[i] = MemoryBank[j];
you are "throwing away" the previous reference to the instance of the class held in MemoryBank[i], and causing both MemoryBank[i] and MemoryBank[j] to "point" to the same instance of a CUserMem class. If you then alter the content of the instance via either reference [i] or [j] you will appear to alter both instances, because they are the same. This is difficult to describe without diagrams, but if you single step the example above through the debugger, you will see what I mean.

No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.

This message is made of fully recyclable Zeros and Ones

GeneralRe: C# List problem Pin
Charlesh331-Aug-09 17:26
Charlesh331-Aug-09 17:26 
GeneralRe: C# List problem Pin
OriginalGriff31-Aug-09 21:36
mveOriginalGriff31-Aug-09 21:36 
Questionfinding .resx fallback culture thru code [modified] Pin
yanairon30-Aug-09 6:00
yanairon30-Aug-09 6:00 
Question[Answered] Creating a list/collection containing different object types [modified] Pin
Trollslayer30-Aug-09 5:24
mentorTrollslayer30-Aug-09 5:24 
AnswerRe: Creating a list/collection containing different object types Pin
PIEBALDconsult30-Aug-09 5:34
mvePIEBALDconsult30-Aug-09 5:34 
GeneralRe: Creating a list/collection containing different object types Pin
Trollslayer30-Aug-09 6:16
mentorTrollslayer30-Aug-09 6:16 
GeneralRe: Creating a list/collection containing different object types Pin
Luc Pattyn30-Aug-09 6:29
sitebuilderLuc Pattyn30-Aug-09 6:29 
GeneralRe: Creating a list/collection containing different object types Pin
Trollslayer30-Aug-09 6:54
mentorTrollslayer30-Aug-09 6:54 
GeneralRe: Creating a list/collection containing different object types Pin
Luc Pattyn30-Aug-09 10:26
sitebuilderLuc Pattyn30-Aug-09 10:26 
QuestionUDP - Server (EndReceiveFrom) doesn't work Pin
softwarejaeger30-Aug-09 5:16
softwarejaeger30-Aug-09 5:16 
QuestionJob market Aussie ??? Pin
UBX30-Aug-09 4:02
UBX30-Aug-09 4:02 
AnswerRe: Job market Aussie ??? Pin
Abhishek Sur30-Aug-09 6:29
professionalAbhishek Sur30-Aug-09 6:29 
GeneralRe: Job market Aussie ??? Pin
UBX30-Aug-09 9:08
UBX30-Aug-09 9:08 
GeneralRe: Job market Aussie ??? Pin
DaveyM6930-Aug-09 11:11
professionalDaveyM6930-Aug-09 11:11 
AnswerRe: Job market Aussie ??? Pin
Joe Woodbury30-Aug-09 11:38
professionalJoe Woodbury30-Aug-09 11:38 
GeneralRe: Job market Aussie ??? Pin
UBX30-Aug-09 14:22
UBX30-Aug-09 14:22 
QuestionHow to assign value to textbox after it get focused in mshtml Pin
Mohsen Esmailpour30-Aug-09 3:34
professionalMohsen Esmailpour30-Aug-09 3:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.