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.exerciseNam e eName,
int weight,int sets,int reps,DateTime startTime,DateTime endTime)
{
Exercise e = new Exercise(eType,eName,start Time,endTi me,weight, reps,sets) ;
listOfExercises.Add(e.Name ,e);
this.fitnessHistory.update FitnessHis tory(e);
}
the FitnessHistory class looks like this:
public class FitnessHistory
{
Hashtable bestPerformance;
Hashtable averagePerformanceIncrease ;
public FitnessHistory()
{
this.bestPerformance = new Hashtable();
this.averagePerformanceInc rease= new Hashtable();
}
public void updateFitnessHistory(Exerc ise e)
{
if(!bestPerformance.Contai nsKey(e.Na me))
{
bestPerformance.Add(e.Name ,e);------ ---->e.Nam e 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.Nam e,e);
return;
}
else
{
if(((Exercise)(bestPerform ance[e.Nam e])).Weigh t>e.Weight )
System.Console.WriteLine(" New entry is greater");
}
updateAveragePerformanceIn crease(e.N ame,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.UPDATEFITNE SSHISTORY( 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 !
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.exerciseNam
int weight,int sets,int reps,DateTime startTime,DateTime endTime)
{
Exercise e = new Exercise(eType,eName,start
listOfExercises.Add(e.Name
this.fitnessHistory.update
}
the FitnessHistory class looks like this:
public class FitnessHistory
{
Hashtable bestPerformance;
Hashtable averagePerformanceIncrease
public FitnessHistory()
{
this.bestPerformance = new Hashtable();
this.averagePerformanceInc
}
public void updateFitnessHistory(Exerc
{
if(!bestPerformance.Contai
{
bestPerformance.Add(e.Name
---------->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
return;
}
else
{
if(((Exercise)(bestPerform
System.Console.WriteLine("
}
updateAveragePerformanceIn
}
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.UPDATEFITNE
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.