PC Review


Reply
Thread Tools Rate Thread

CollectionBase Update

 
 
Franck Diastein
Guest
Posts: n/a
 
      10th Feb 2005
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9zZW1h?=
Guest
Posts: n/a
 
      10th Feb 2005
//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" wrote:

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

 
Reply With Quote
 
=?Utf-8?B?Sm9zZW1h?=
Guest
Posts: n/a
 
      10th Feb 2005
Sorry Frank, I forget the Add_ Method

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

Josema.

"Josema" wrote:

> //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" wrote:
>
> > 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
> >

 
Reply With Quote
 
Franck Diastein
Guest
Posts: n/a
 
      10th Feb 2005
Thank you !!!

King regards

Josema wrote:
> //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" wrote:
>
>
>>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
>>

 
Reply With Quote
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      10th Feb 2005
Franck Diastein wrote:
> 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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML and collectionbase Mark Winters Microsoft C# .NET 3 17th Jun 2011 08:33 AM
CollectionBase wg Microsoft C# .NET 3 9th Feb 2006 11:39 AM
CollectionBase =?Utf-8?B?Sm9zZW1h?= Microsoft C# .NET 1 26th Apr 2005 02:21 AM
CollectionBase Johan Karlsson Microsoft VB .NET 5 2nd Mar 2004 06:03 PM
CollectionBase bug? m. pollack Microsoft Dot NET 7 12th Jan 2004 10:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 PM.