Removing items from NVL

Removing items from NVL

Old forum URL: forums.lhotka.net/forums/t/1009.aspx


kasvis posted on Thursday, August 24, 2006

I am having some problems with the NVLs  I am retrieving a list of employees to be assigned to Project. While assigning the users I want to remove users that have already been added to the project.

So i included a Remove method to the NVL object but its giving an stackoverflow exception. I am not sure how to resolve this issue. I have included the code below .

Code added to the NVL object

public void Remove(int key)

{

if (ContainsKey(key))

{

   this.Remove(key);

}

}

Code in the Form displaying the Users. _usrlst is the child object of the Project that contains the list of users already assigned.

foreach (User usr in _usrlst)

{

   _users.Remove(rm.UserID);

}

 

 

I would appreciate any help in resolving this issue.

Thanks

 

david.wendelken replied on Thursday, August 24, 2006

I would have to see your entire class to have a hope of telling you exactly where it went wrong.

However, if I remember correctly on the RoleList sample class, the class maintains a static copy of the list to avoid hitting the database over and over again.  It's possible that another object already removed the key you think is there, if you implemented your class in a similar manner.

 

kasvis replied on Thursday, August 24, 2006

Thanks for responding David.

>>It's possible that another object already removed the key you think is there

In order to make sure that an exception is not thrown, I check to see if the object contains the key before attempting to remove it.

I am not sure why I am getting stackoverflow exception though. seems totally unrelated.

thanks

xal replied on Thursday, August 24, 2006

It's ok to check if it exists.
The problem is that your doing:

public void Remove(int key)

{

if (ContainsKey(key))

{

   this.Remove(key); // <--- This is calling the same function that started the whole thing.

}

}


The same operation is running indefinitely, untill the stack overflows...

You should be calling base.Remove(key)

Andrés

kasvis replied on Thursday, August 24, 2006

Thanks Andres. That was pretty dumb of me.

Finally got it to work with the code below.  Might not be most efficient but does the job.

public void Remove(int key,string value)

{

foreach (NameValuePair item in this)

{

if (item.Key == key && item.Value == value)

{

IsReadOnly = false;

this.Remove(item);

IsReadOnly = true;

return;

}

}

}

Copyright (c) Marimer LLC