J
Joe Cool
I am having a strange problem with a generic list. Maybe someone can
spot my error. Specifically I am trying to remove an item when the
list is a list of class object instances.
public class myClass
{
public string myString { get; set; }
public int myInt { get; set; }
public myClass()
{
this.myString = string.Empty;
this.myInt = 0;
}
}
public class DoSomeWork
{
public List<myClass> myList = new List<myClass>();
public DoSomeWork()
{
this.myList = new List<myClass>();
}
public void SomeWork()
{
myClass myClassInstance = null;
myClassInstance = new myClass();
myClassInstance.myString = "ABCDEF";
myClassInstance.myInt = 10;
this.myList.Add(myClassInstance);
myClassInstance = new myClass();
myClassInstance.myString = "GHIJKL";
myClassInstance.myInt = 20;
this.myList.Add(myClassInstance);
myClassInstance = new myClass();
myClassInstance.myString = "MNOPQR";
myClassInstance.myInt = 35;
this.myList.Add(myClassInstance);
}
public void RemoveItem(string stringval, int intvalue)
{
myClass myClassInstance = null;
myClassInstance = new myClass(); /* breakpoint here */
myClassInstance.myString = stringval;
myClassInstance.myInt = intvalue;
this.myList.Remove(myClassInstance);
}
}
Later, in the application's main form this Button control Click event:
public partial class Form1 : Form
{
private DoSomeWork doSomeWork;
public Form1()
{
InitializeComponent();
this.doSomeWork = new DoSomeWork();
this.doSomeWork.SomeWork();
}
...
private void button1_Click(object sender, EventArgs e)
{
this.doSomeWork.RemoveItem("GHIJKL", 20);
}
}
I set a breakpoint as indicated in the RemoveItem method and run in
Debug mode. I click on the button
and when the breakpoint hits, I examine the Count property of the
myList object and the value is 3
as expected.
I then single step through the RemoveItem method call. I then re-
examine the Count property of the
myList object and it is still 3.
I would expect it to be 2.
Any thoughts?
spot my error. Specifically I am trying to remove an item when the
list is a list of class object instances.
public class myClass
{
public string myString { get; set; }
public int myInt { get; set; }
public myClass()
{
this.myString = string.Empty;
this.myInt = 0;
}
}
public class DoSomeWork
{
public List<myClass> myList = new List<myClass>();
public DoSomeWork()
{
this.myList = new List<myClass>();
}
public void SomeWork()
{
myClass myClassInstance = null;
myClassInstance = new myClass();
myClassInstance.myString = "ABCDEF";
myClassInstance.myInt = 10;
this.myList.Add(myClassInstance);
myClassInstance = new myClass();
myClassInstance.myString = "GHIJKL";
myClassInstance.myInt = 20;
this.myList.Add(myClassInstance);
myClassInstance = new myClass();
myClassInstance.myString = "MNOPQR";
myClassInstance.myInt = 35;
this.myList.Add(myClassInstance);
}
public void RemoveItem(string stringval, int intvalue)
{
myClass myClassInstance = null;
myClassInstance = new myClass(); /* breakpoint here */
myClassInstance.myString = stringval;
myClassInstance.myInt = intvalue;
this.myList.Remove(myClassInstance);
}
}
Later, in the application's main form this Button control Click event:
public partial class Form1 : Form
{
private DoSomeWork doSomeWork;
public Form1()
{
InitializeComponent();
this.doSomeWork = new DoSomeWork();
this.doSomeWork.SomeWork();
}
...
private void button1_Click(object sender, EventArgs e)
{
this.doSomeWork.RemoveItem("GHIJKL", 20);
}
}
I set a breakpoint as indicated in the RemoveItem method and run in
Debug mode. I click on the button
and when the breakpoint hits, I examine the Count property of the
myList object and the value is 3
as expected.
I then single step through the RemoveItem method call. I then re-
examine the Count property of the
myList object and it is still 3.
I would expect it to be 2.
Any thoughts?