0

The flow of code doesn't follow my logic, and my lists don't get the required output.

public static List<String> genericList;
public static List<String> List1;

in frmLoadCurrentForm():

List1 = new List<String>();
genericList = new List<String>();

Then, in various places (? buttons)

1 private void btn1_Click(object sender, EventArgs e)
2 {
3     genericList = List1.ToList();
4     call(); 
5     List1 = genericList.ToList();
6 }
private void call()
{
    frmForm form = new frmForm();            
    for (int i = 0; i < genericList.Count(); i++)
        form.lst.Items.Add(genericList.ElementAt(i));
    form.Show();
    //form updates genericList on exit with lst contents, tested
}

While stepping through my code, I put a breakpoint on line 5, and code goes to line 5 then 4 (and into the form), then doesn't go back to 5 - so my List1 doesn't get updated with what happens inside the call().

I don't understand this logic, or what to do about it.

I am using multiple lists and a single genericList so that I can use a complicated interface for multiple situations, and theoretically my logic seems valid...

At the exit from the form, genericList has the right info, List1 doesn't. Why ?

Edit: added declaration of lists

Edit: As I step through, the form will start rendering but will not finish until the code at the end of the button that called the method that created the form got executed. Which is even more odd, since the form instance is created in a method so it should be closed and disposed of at the end of the method...

Thalia
  • 13,637
  • 22
  • 96
  • 190

3 Answers3

1

Well, by putting a breakpoint at Line 5, you're not going to be running line 5; but the call on line 4 will still go through, and the program will proceed normally. Line 4, you called a function, which effectively ran, then stopped when it came back.

Breakpoints are only triggered when the program executes that specific line. C# doesn't execute code linearly, it can, but it is not held to that rule.

Take a look at http://www.ndepend.com/ It's a mapping software, and it might be able to give you a better idea, visually.

Please inform me if my answer is wrong, as well, therefore I do not spread incorrect information. Thanks!

plast1K
  • 469
  • 3
  • 13
  • I don't think I can install NDepend, though it is tempting... My point with the breakpoints was, the line on 5 was hit before the one on 4. Assuming that the program stopped just before, I should have been able to step over line 5 and execute it - but I didn't - went on to the next function. Putting a breakpoint in the next function, I could clearly see that List1 was still empty, while genericList still had items. – Thalia Jul 18 '12 at 20:32
  • Hmm, maybe I've just been at work too long, but I'm getting confused. Perhaps I'm not even in the ballpark of the question you need answered. Haha. Anyway, I've seen debugging hit breakpoints after calls and "go back", which is strange. Not sure why or how it functions like that. It does appear to me that a breakpoint on 5 would not allow data to be added to List1, though; as it gets added in the following function and never gets set to List1. I might just be imagining things, though. – plast1K Jul 18 '12 at 20:42
  • sorry, I don't understand. My brain says, if the breakpoint is on List1, I should be able to step over and get to "6 }" and have something in List1. But the steps I see (no matter where I set breakpoints) are 5, 4, 6, and nothing gets into List1. Is that logical ? – Thalia Jul 18 '12 at 20:49
1

Although I am confused what you are doing with the above code you should be aware that Form.Show() only returns if the opened Form has been closed - maybe that's a source of your confusion.

Further I don't know what you want to do with the call to the method ToList() because the purpose of this method is to generate a list object from an IEnumerable object but you seem to generate a List from the List which doesn't make sense to me.

I have not tested your code but I think you code should execute as it's written above without any problems that means when you click the button that has the event handling method btn1_Click been called execution should be as follows:

  1. execution of line 3
  2. control goes in to the code within the call() block and proceeds sequentially until the Form.Show() call. This call lets the form be displayed and the execution is branched again to the internal Form code.
  3. When the form has been closed execution returns to the first line after the Form.Show() call which is the closing braces of the method.
  4. The next statement to be executed is line 5 of your event handling method.

When you put a breakpoint at the declaration line of the btn1_Click method and step through the code in a step by step manner you should experience exactly this behavior.

marc wellman
  • 5,808
  • 5
  • 32
  • 59
  • What you say is very logical, and it is exactly what I expect. The ToList() is what I found that would copy one list to another (I don't want to insert, and I thought initially that assigning list1=list2 might have been what was causing my bug). On the close() of the frmForm I update genericList... Theoretically it all should go the way you describe. I guess there is no error, it just doesn't work, so I will simply have to scrub it and change the approach. – Thalia Jul 18 '12 at 21:10
  • @emptyheaded with `list1 = list2` you are copying nothing but instead you are creating a new reference that points to your existing list. Try to decide whether you want list2 to be a shallow copy of list1 or a deep copy - I don't know your context but for some situations this is a highly critical question. – marc wellman Jul 18 '12 at 21:22
  • The lists just contain strings, but I do want a deep copy, that's why i used the .ToList() - is it correct ? I though about using a loop, and will just use one anyway... instead of smart shortcuts... – Thalia Jul 18 '12 at 21:28
  • @emptyheaded According to this (http://stackoverflow.com/q/2774099/946904) post the method ToList() only performs a shallow copy. – marc wellman Jul 18 '12 at 21:35
  • I did a for loop to copy the list items. Still not getting the form and the action in the correct order. It may be something to do with GUI threads ? – Thalia Jul 18 '12 at 23:50
1

It was definitely a threading problem.

To solve it the easiest way possible (instead of creating a separate thread), I replaced form.Show() with form.ShowDialog();

Thalia
  • 13,637
  • 22
  • 96
  • 190
  • 1
    Rather than "a threading problem", it's probably clearer to say that it was a synchronicity problem - you expected `form.Show()` to be synchronous (i.e. that the code would stop execution until the form was closed) whereas it was really asynchronous. – Simon MᶜKenzie Jul 19 '12 at 04:50