Hashtable keys clash does not allow removing ....
Posted on 2005-05-05
in class clubmember I do the following:
public class ClubMember
public ClubMember(string name)
this.listOfExercises = new Hashtable();
public void addExercise (Exercise.exerciseType eType,Exercise.exerciseName eName,
int weight,int sets,int reps,DateTime startTime,DateTime endTime)
Exercise e = new Exercise(eType,eName,startTime,endTime,weight,reps,sets);
the FitnessHistory class looks like this:
public class FitnessHistory
this.bestPerformance = new Hashtable();
this.averagePerformanceIncrease= new Hashtable();
public void updateFitnessHistory(Exercise e)
bestPerformance.Add(e.Name,e);---------->e.Name is exercise name and is key
---------->e is actual exercise object which obv
---------> will also contain e.Name but I am -------> passing e.Name separate so I can make that a key.
System.Console.WriteLine("New entry is greater");
What do I do with this code:
As soon as a new exercise object is created in ClubMember class (using the method addExercise(....))
I am adding the new exercise object to the listOfExercises in the CLubMember.
but I also want to keep an updated history of everything that is being done so as soon as the exercise is added to the listOfExercises,
I am calling FITNESSHISTORY.UPDATEFITNESSHISTORY(exercise object);
updateFitnessHistory () is a simple method really .
Will check the keys in the hashtable and if it DOES not find the e.Name being passed it will add that e.Name as a key and the associated exercise object as value.
IF IT DOES find the e.Name, then it will check for some things between the current exercise object and the stored exercise object and remove or not remove.
but when my program execution reaches this.fitnessHistory(e)
it breaks IF the e.Name from this exercise object is already present in the hashtable bestPerformance and the exception throw is ArgumentException.
I can understand that hashtables might not allow duplicate keys but I AM SPECIFICALLY CHECKING FOR THE DUPLICATE KEY in the updateFitnessHistory() object and THEN removing or inserting it.
maybe I dont need to initialize the hashtables in the constructor of the FitnessHistory class ..... maybe I should delcare and intialise them in main class declaration.
btw please ignor the averagePerformance method ...thats another story all together !
Please help !