?
Solved

Hashtable keys clash does not allow removing ....

Posted on 2005-05-05
3
Medium Priority
?
261 Views
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);
                  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 !
0
Comment
Question by:deersuper
1 Comment
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 13941223
I am assuming there is alot of other code here ...

just one thing I notice quickly ...

               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);


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?

Greg


0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 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