Strange Bug with the order of objects in a collection

G

Guest

I've come across a problem that I just cannot explain. I'm hoping that
someone has seen it before.

I have the following [basic] data structure:

Questions (collection)
Question (object)
Choices (collection)
Choice (object)


I populate Questions[0].Choices with 4 Choice objects - let's call them "A",
"B", "C", "D". They're inserted into the Choices collection in that
order. Then I rearrange the order this way: B C A D I pause
the program and use "QuickWatch" to confirm that the order has indeed
changed. Everything's fine so far.

Then I run a few more lines of code - none of which are changing this data
in ANY WAY and yet when I check back to look at Choices, the order has
changed to: A C A D

I couldn't believe this was happening!!! But I've checked it numerous times
and it IS happening! I've triple checked the lines of code and they are
definitely NOT touching this data model. It almost seems like that "B" is
being changed to an "A" after a certain length of time or else something
external is doing it. But it definitely isn't my code.

How on earth is this possible???
 
B

Bruce Wood

First, you need to post the relevant code.

As a blind stab at a solution, I would ask whether it's not a reference
problem. To explain what I mean, here's an example:

Choice temp = new Choice("A");
questions[0].Choices.Add(temp);
temp = new Choice("B");
questions[0].Choices.Add(temp);
Choice temp2 = new Choice("C");
questions[0].Choices.Add(temp2);
temp2 = new Choice("D");
questions[0].Choices.Add(temp2);

.... now for the kicker ...

temp.ChoiceLetter = "A"; // or something like that

.... Hey! The "B" in the list changed to "A"! Of course, because I was
still holding a reference to the object I created and I modified it,
since the reference in the list points to the same object, it changed
"too" (it didn't change "too": there's only one of them).

Could it be something like that?

Anyway, you need to post the code. :)
 
G

Guest

Thanks, Bruce. I actually had a reference problem a few weeks ago but this
time it was not that. About half an hour after posting and after talking
with a friend of mine, I discovered the culprit. I had sort of outsmarted
myself. I have built some pretty extensived automated update code that keeps
a form in sync with the data model. I call it "ASM". I didn't think this
was the cause because I was stepping through the code in question and it
wasn't showing me the execution of any ASM code.

But lo and behold it was firing! I still don't know why the debugger
doesn't show it normally but once I added a few breakpoints in the ASM code I
saw it was so.

So the solution was to temporarily disable this code, do the necessary
visual updates, and then reactivate the code.

Sorry to trouble you [all] but I was completely stymied ... and have learned
something new about the debugger!

Thank you again for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top