473,379 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

CollectionBase Update

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
Nov 16 '05 #1
4 1403
//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

Nov 16 '05 #2
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

Nov 16 '05 #3
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

Nov 16 '05 #4
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#)
------------------------------------------------------------------------
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve M | last post by:
I have subclassed CollectionBase. I have also implemented GetEnumerator(). I have tried to set the DataSource of a DataGrid to an instance of my subclass. However, the items in the grid are not...
7
by: m. pollack | last post by:
Hi all, I've been using the CollectionBase class to implement a strongly-typed collection, but I have noticed that the RemoveAt method does not seem to call the "On" hook methods (OnRemove,...
2
by: m.pollack | last post by:
Hi all, I have an application which uses a class object that contains a collection. In order to use the PropertyGrid control to expose properties to the user at runtime, I created a...
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
0
by: LIJO CHEERAN | last post by:
Hello friends I am trying to study about CollectionBase. I have inherited CollectionBase in the class TheCollection.cs. I am using the “TheCollection. in an aspx page to store objects...
1
by: jc | last post by:
RE: Why use a CollectionBase class here vs dataset or dataview? I'm looking at some vb.net 2005 code that was generated from a homegrown Codesmith Template that generate all of the retreival and...
3
by: Tony Johansson | last post by:
Hello! Sorry for opening up this task again. I want to fully understand this List that is return from CollectionBase. According to you is List in CollectionBase implemented something like...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.