Lists basics
-
im an artist and i code for fun. I just encounter a strange problem in my code and i had to debug it a lot to find this little bug. Not a bug per say, but more like my way of using things (probably wrong). But now is an eye opener.
//What is the diference between these 2 codes?
List block = new List();
List temp = new List();
void SomeEvent
{
//code1
block.Clear();
block.AddRange(temp);//code2
block = temp;
}I wanted the text from [temp] list to copy into [block] list. My usual way of doing it, was as in code2. But it seems is a bad way. Because when temp is cleared, also the block is cleared, like block is a pointer to the temp. I only wanted a temporary transfer of data between the 2 lists and not a permanent attachment. Yah... can you explain the real difference? I had to learn it the hard way i suppose. :)
-
im an artist and i code for fun. I just encounter a strange problem in my code and i had to debug it a lot to find this little bug. Not a bug per say, but more like my way of using things (probably wrong). But now is an eye opener.
//What is the diference between these 2 codes?
List block = new List();
List temp = new List();
void SomeEvent
{
//code1
block.Clear();
block.AddRange(temp);//code2
block = temp;
}I wanted the text from [temp] list to copy into [block] list. My usual way of doing it, was as in code2. But it seems is a bad way. Because when temp is cleared, also the block is cleared, like block is a pointer to the temp. I only wanted a temporary transfer of data between the 2 lists and not a permanent attachment. Yah... can you explain the real difference? I had to learn it the hard way i suppose. :)
When you do this:
block = temp;
you don't copy the elements, you copy the reference. In other words, you throw away both the existing value in
block
, and the actual ilist itself. The AddRange method adds each individual item to the existing collection - if you like, it copies the collection instead of changing the references. Have a look at this: Using struct and class - what's that all about?[^] - it gets a little more advanced than you are asking at the moment, but the early stuff should help."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
im an artist and i code for fun. I just encounter a strange problem in my code and i had to debug it a lot to find this little bug. Not a bug per say, but more like my way of using things (probably wrong). But now is an eye opener.
//What is the diference between these 2 codes?
List block = new List();
List temp = new List();
void SomeEvent
{
//code1
block.Clear();
block.AddRange(temp);//code2
block = temp;
}I wanted the text from [temp] list to copy into [block] list. My usual way of doing it, was as in code2. But it seems is a bad way. Because when temp is cleared, also the block is cleared, like block is a pointer to the temp. I only wanted a temporary transfer of data between the 2 lists and not a permanent attachment. Yah... can you explain the real difference? I had to learn it the hard way i suppose. :)
The first case deals with items "in the bucket". In the second, you're replacing the original "block bucket", and pointing it to the same bucket that temp is pointing to.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food