Link to home
Start Free TrialLog in
Avatar of deersuper
deersuper

asked on

Hashtable keys clash does not allow removing ....

in class clubmember I do the following:

public class ClubMember
      {
            
            FitnessHistory fitnessHistory;
            AdminProfile _adminProfile;
            Hashtable listOfExercises;

            public ClubMember(string name)
            {
                  this._adminProfile=new AdminProfile(name);      
                  this.fitnessHistory=new FitnessHistory();
                  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);
                  listOfExercises.Add(e.Name,e);
                  this.fitnessHistory.updateFitnessHistory(e);
            }

the FitnessHistory class looks like this:

public class FitnessHistory
      {
            Hashtable bestPerformance;
            Hashtable averagePerformanceIncrease;
            

            public FitnessHistory()
            {
                  this.bestPerformance = new Hashtable();
                  this.averagePerformanceIncrease= new Hashtable();
            }
            
            public void updateFitnessHistory(Exercise e)
            {
                  if(!bestPerformance.ContainsKey(e.Name))
                  {
                        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.
                        averagePerformanceIncrease.Add(e.Name,e);
                        return;
                  }
                  else
                  {
                        if(((Exercise)(bestPerformance[e.Name])).Weight>e.Weight)
                              System.Console.WriteLine("New entry is greater");
                  }
                  
                  updateAveragePerformanceIncrease(e.Name,e.Date,e.Weight);
            }

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 !
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial