Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining collections-PLEASE HELP 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,I hope you can help me,coz I'm stuck.
I can't understand some things about defining collections.
Here is an example

public class Animals:CollectionBase
{
public void Add(Animal NewAnimal);
{
List.Add(newAnimal);
}

}

a.How can CollectionBase class implement IList interface if it doesn't implement ALL of IList's methods?How is that possible?

b.List property returns a varible of type IList,which points to an object from which this List property was called from?Right?

c.How can I use List.Add() method if I haven't defined it yet?Where is this method defined?


I hope you will find some time to help me and for that I thank you

 
(a) I haven't gotten that far into writing my own collections (just used the ones provided), but from what I can tell, implementing all the methods provided (specified?) by IList isn't required. I suspect, however, if you fail to provide an implementation of each of them, your code will fail in mysterious ways later on when you try & interact with other parts of the framework.

(b) Don't know.

(c) I think it'll pick up another List method somewhere in the inheritance tree. If you don't like the behavior of this default method, you'll have to provide one yourself.

Chip H.
 
Thank you for your help.But does somebody know the answers?I really need to know this.

Thank you
 
if you derive a class from some base, the methods provided by the base are automatically derived as well, unless you override them...then your methods take precedence. c#'s inheritance model is similair to c++'s.

you can see a list of all of the base members (if you're using vs.net) by typing

this.

inside of a function and all of the members of the class will appear in a drop down box.


what i would do would be to do this (sorry if there are spelling/capitalization errors):

public class animallist : CollectionBase
{
//setup the class by calling base constructor and setting innerlist (derived from collectionbase) to a new arraylist
public animallist() : base()
{
this.InnerLIst = new ArrayList();
}
//overloaded construcotr...not necessary
public animallist(ArrayList l) : base()
{
this.InnerList = l;
}

//don't let user see Ilist implementation...force them to use arraylist provided by CollectionBase dervied class (this is actually a 3rd level class)
protected override IList List;

public void Add(animal a) //define this yourself
{
this.InnerList.Add(a); //InnerList is derived property
}

public animal Get(int i)
{
return (animal)this.InnerList; //make the class useful by being able to extract an animal at a certain location in the array
}

//expose the array if you want to be able to do other stuff like searching
public ArrayList animalarray
{
get {
return InnerList;
}
}
}



i hope this helps a little bit mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top