Wednesday, September 8, 2010

Index was out of range (Lazy Initialization bug)

Index was out of range. Must be non-negative and less than the size of the collection.


Visual Studio stops on the following line in the Designer.cs file for the form:

base.Dispose(disposing);

It happens in one of the case when a Form contains a Tabcontrol with two tab pages and the second page has the Gridview . After opening the form ,when we try to close the form without going to the second tab gives the error.

This looks like a classic lazy initialization bug. The workaround is crude but effective:


private void
TestForm_Load(object sender, EventArgs e) {
this.tabControl1.SelectedIndex = 1;
this.tabControl1.SelectedIndex = 0;
}





No comments: