Collections Problem

  • Thread starter Thread starter John Bowman
  • Start date Start date
J

John Bowman

Hi All,

I've got a problem with the following example test code crashing,
complaining that the object reference is not set (.NET 1.1). Basically, I
instantiate a
collection of objects and begin populating the collection with appropriate
objects, then print them out. What am I doing wrong to cause the crash?
Seems like it should be obvious, but I'm not seeing it.

private void Form1_Load(object sender, System.EventArgs e)
{
MyObjCollection moc = new MyObjCollection();
MyObj mo1 = new MyObj("Object1");
moc.Add(mo1);
MyObj mo2 = new MyObj("Object2");
moc.Add(mo2);
foreach(MyObj obj in moc)
{
MessageBox.Show(obj.MyVar1);
}
}

internal sealed class MyObj
{
private string m_MyVar1 = "FooBar";
internal MyObj(string MyVar1)
{
m_MyVar1 = MyVar1;
}

internal string MyVar1
{
get
{
return m_MyVar1;
}

}
}

internal sealed class MyObjCollection : BaseCollection
{
internal MyObjCollection()
{
}

internal void Add(MyObj obj)
{
this.List.Add(obj);
}
}


Any help would be much appreciated. TIA,
 
Hi John,
on which line are you seeing this problem. I just copied your code and it
ran fine, well except for having to change BaseCollection to CollectionBase,
not sure if that was a typo or you have some other class you have defined.

Mark.
 
Mark,

Man, do I feel like a dummy. That was it. You hit the nail on the head. I
should be using CollectionBase not BaseCollection. Apparently, I got those 2
base classes mixed up in my head and tried to use the wrong base class to
inherit form.
Many thanks!

John
 
Back
Top