CollectionBase Update

  • Thread starter Thread starter Franck Diastein
  • Start date Start date
F

Franck Diastein

Hi, I have create a TypeSafe collection with CollectionBase, but I don't
know how to update an object... Let's say I have an Animal class with
Animals collection.
Animal has a property named Number that's unique in entire collection...

I would like to do something aka in my form:

private void UpdateAnimal(Animal UpdatedAnimal){

int i = UpdatedAnimal.Number
m_Animals.Update(UpdatedAnimal,i)

}

What is the best way to implement this ? Am I taking the right approach ?

I have googled about the subject, but all samples are too simplistic...

TIA
 
//Hi Franck,
//I think a good ide maybe could be this:

//Make a method to get from collection ani object of type Animal:
public Animal Get_(int id)
{
return (Animal) List[id];
}
//Make a method to remove a object of type animal from the collection
public void Remove_(Animal animaltoremove)
{
List.Remove(animaltoremove);
}
//And then create the Update Method
private void UpdateAnimal(Animal UpdatedAnimal)
{
//get from the collection the correct animal
Animal CurrentAnimal=this.Get_(UpdatedAnimal.Number);
//Remove the current Animal from collection
this.Remove_(CurrentAnimal);
//Add the Updated Animal into the collection
this.Add_(UpdatedAnimal);
}
//Hope this helps.
//Josema.
 
Sorry Frank, I forget the Add_ Method

public void Add_(Animal newanimal)
{
List.Add(newanimal);
}

Josema.

Josema said:
//Hi Franck,
//I think a good ide maybe could be this:

//Make a method to get from collection ani object of type Animal:
public Animal Get_(int id)
{
return (Animal) List[id];
}
//Make a method to remove a object of type animal from the collection
public void Remove_(Animal animaltoremove)
{
List.Remove(animaltoremove);
}
//And then create the Update Method
private void UpdateAnimal(Animal UpdatedAnimal)
{
//get from the collection the correct animal
Animal CurrentAnimal=this.Get_(UpdatedAnimal.Number);
//Remove the current Animal from collection
this.Remove_(CurrentAnimal);
//Add the Updated Animal into the collection
this.Add_(UpdatedAnimal);
}
//Hope this helps.
//Josema.

Franck Diastein said:
Hi, I have create a TypeSafe collection with CollectionBase, but I don't
know how to update an object... Let's say I have an Animal class with
Animals collection.
Animal has a property named Number that's unique in entire collection...

I would like to do something aka in my form:

private void UpdateAnimal(Animal UpdatedAnimal){

int i = UpdatedAnimal.Number
m_Animals.Update(UpdatedAnimal,i)

}

What is the best way to implement this ? Am I taking the right approach ?

I have googled about the subject, but all samples are too simplistic...

TIA
 
Thank you !!!

King regards
//Hi Franck,
//I think a good ide maybe could be this:

//Make a method to get from collection ani object of type Animal:
public Animal Get_(int id)
{
return (Animal) List[id];
}
//Make a method to remove a object of type animal from the collection
public void Remove_(Animal animaltoremove)
{
List.Remove(animaltoremove);
}
//And then create the Update Method
private void UpdateAnimal(Animal UpdatedAnimal)
{
//get from the collection the correct animal
Animal CurrentAnimal=this.Get_(UpdatedAnimal.Number);
//Remove the current Animal from collection
this.Remove_(CurrentAnimal);
//Add the Updated Animal into the collection
this.Add_(UpdatedAnimal);
}
//Hope this helps.
//Josema.

:

Hi, I have create a TypeSafe collection with CollectionBase, but I don't
know how to update an object... Let's say I have an Animal class with
Animals collection.
Animal has a property named Number that's unique in entire collection...

I would like to do something aka in my form:

private void UpdateAnimal(Animal UpdatedAnimal){

int i = UpdatedAnimal.Number
m_Animals.Update(UpdatedAnimal,i)

}

What is the best way to implement this ? Am I taking the right approach ?

I have googled about the subject, but all samples are too simplistic...

TIA
 
Franck said:
Hi, I have create a TypeSafe collection with CollectionBase, but I don't
know how to update an object... Let's say I have an Animal class with
Animals collection.
Animal has a property named Number that's unique in entire collection...

I would like to do something aka in my form:

private void UpdateAnimal(Animal UpdatedAnimal){

int i = UpdatedAnimal.Number
m_Animals.Update(UpdatedAnimal,i)

}

What is the best way to implement this ? Am I taking the right approach ?

BEST way is to override Equals in Animal and implement it so it
compares the Number properties of the current animal and the one passed in.

With that, you can then do:
m_Animals[m_Animals.IndexOf(UpdatedAnimal)].Number = newValue;

IndexOf, Contains, they work all with the Equals method of the object
contained in the collection.

Frans

--
 
Back
Top