Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ComboBox doesn't throw an Exception when setting DataSource?

First my code. I have a class:

class Person {
    public int Id { set; get; }
    public string Name { set; get; }
    public Person(int i, string n) {Id = i; Name = n;}
}

and a ComboBox object:

this.comboBox_Persons = new System.Windows.Forms.ComboBox();

and somewhere in my code:

List<Person> persons = new List<Person>() {
    new Person(5, "John"),
    new Person(8, "Mike")
};
comboBox_Persons.Items.Clear();
comboBox_Persons.DisplayMember = "Name";
comboBox_Persons.ValueMember = "Id";
comboBox_Persons.DataSource = persons;

and a static member and an event handler:

public static string test = "";

void comboBox_PersonsSelectedIndexChanged(object sender, EventArgs e)
{
    test = test + "1";
    string id = (string) comboBox_Persons.SelectedValue;
}

The last line is wrong (it comes from an earlier version of my code) and it should be:

int id = (int) comboBox_Persons.SelectedValue;

and it took me some time to realize that my problem is in this line.

But my question is about the wrong version of the line and exactly about the code above.

Why no exception is thrown when comboBox_Persons.DataSource is beeing filled?

When my form is ready comboBox_Persons contains two items and it should display "John" and "Mike" but it does not. The combobox displays the type name (with namespace) of the class Person twice. Moreover, the value of the static field 'test' is "11" which means that the event handler has been invoked. But the next line (with casting to string) should throw an exception, but it does not. Why? And next, when I click on the combobox and change the selected value then the event handler is called and an exception is thrown (telling that it can't cast to string).

So, why combobox does not throw any exception when setting DataSource?

And why combobox displays type names instead of defined property 'Name'?

I wonder why this control behaves this way and I didn't find any answer in .NET documentation and on the Internet.

When I change the wrong line to the correct version everything is OK.

like image 235
Victor Avatar asked Oct 21 '22 19:10

Victor


1 Answers

I was searching on google for the combobox source code to see how it is implemented and why exceptions are getting buried and I found this:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/69a0b831-7782-4bd9-b910-25c85f18bceb/visual-studio-doesnt-break-on-unhandled-exception-with-windows-64bit?forum=vsdebug

http://social.msdn.microsoft.com/Forums/vstudio/en-US/8a7006a1-ad86-4aec-9604-d7ccf99ce00b/selectedindexchanged-handler-exception-treated-as-first-chance?forum=vsdebug

Interestingly enough, if you add a line to throw an exception in your SelectedIndexChanged event, the application doesn't crash!!

private void comboBox_persons_SelectedIndexChanged(object sender, EventArgs e)
    {
        test = test + "1";
        string id = (string)comboBox_Persons.SelectedValue;
        throw new ApplicationException("Test");
    }

Apparently, this is an issue with 64 bit machines and there is a hotfix available for this issue. As stated in this post, exception prone code in Form_Load event doesn't break the application!! Similar is the case with combobox SelectedIndexChanged event.

I couldn't test the hotfix since I do not have Win 7 SP1 installed on my machine. However, from the comments in the blog, the hotfix doesn't actually fix all the issues with exceptions being buried in 64 bit machines.

I hope this information is helpful!

like image 89
Poornima Avatar answered Nov 15 '22 07:11

Poornima