Help with generic list

Asked By John O'Hara
09-Feb-10 11:12 AM
Earn up to 0 extra points for answering this tough question.

I have a generic list which contains a class called Parameter.

Parameter contains 2 integers, ParamType and ParamValue.

A generic list is built containing a small number of values List<Parameter> myList

I have created a function which passes in 2 parameters.  These are passed individually but are used to create a new Parameter within the function.  The Parameter is tested against myList, and if it is found removed.

In order to do this I have used the remove function, the syntax being:

myList.Remove( new Parameter(ParamType, ParamValue));

The remove isn't working and at present I'm using LINQ to do it.  I would apprecaite it if someone could point out the error with the remove function so that I can correctly code it without LINQ.

Thanks

John

  Sorry, posted with wrong account. I don't believe your code will work

Robbe Morris replied to John O'Hara
09-Feb-10 11:20 AM
myList.Remove( new Parameter(ParamType, ParamValue));

is testing against a newly created object that doesn't have a reference pointer to an item that actually exists in your list.

  re: Help with generic list

Huggy Bear replied to John O'Hara
09-Feb-10 11:53 PM
Yes Robbe is spot on. You are storing a different instance and while removing you are passing another, this will not work.

But how about using a Generic Dictionary instead of List, here you will get the benefit of key value stuff while storing the parameter class object form the key based on the two parameter values, like

//use this key while storing
string key = string.Format("{0}{1}", param1, param2);

//while removing form the key based on the parameter values again
dictionaryObject.Remove(key);


Create New Account