Hashtable keys clash does not allow removing ....

Posted on 2005-05-05
Last Modified: 2010-08-05
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);

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)
                        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,
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 !
Question by:deersuper
    1 Comment
    LVL 37

    Accepted Solution

    I am assuming there is alot of other code here ...

    just one thing I notice quickly ...

                        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.

    you change averagePerformanceIncrease as well as bestPerformance without checking the averagePerformanceIncrease to see if it already contains the key...

    also in the other class you do listOfExercises.Add(e.Name,e); without checking it ...

    can you provide a small class set that issues this same behavior and can be run here?



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now