How Implement IList Generic Interface


V

Venkatesh Bhupathi

Hi All,

I am trying to inherit the Generic IList<T> interface to form the
typed collection of objects of type T. Following is my sample code,


Public Class Record
{
public string name;
public string address;

Public Record(string sName, string sAddress)
{
this.name = sName;
this.address = sAddress;
}
}

Public Class Records<Record> : IList<Record>
{
public void Add(Record objRecord)
{
this.Add(objRecord);
}

public void Remove(int index)
{
this.RemoveAt(index);
}

......... with all the explicit member of IList interface
}

Implementation:
------------------------
Records objRecords = new Records();
objRecord.Add(new Record("xyz", "hyd"));


This code compiled fine, but at runtime the this.Add inside the
Records class has thrown StackOverFlowException. I am very new to
using Generics am confused with this error. Please help me understand
if what I am trying to implement is correct or is there any other way
to implement generic custom typed collection of objects.

Please let me know if you need any other information.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

I am trying to inherit the Generic IList<T> interface to form the
typed collection of objects of type T. Following is my sample code,

This code compiled fine, but at runtime the this.Add inside the
Records class has thrown StackOverFlowException. I am very new to
using Generics am confused with this error. Please help me understand
if what I am trying to implement is correct or is there any other way
to implement generic custom typed collection of objects.

It's really nothing to do with generics - your implementation of "Add"
just calls itself.
This would fail whether or not you used generics.
What were you expecting to add the entry to?

What do you need to do that List<T> doesn't give you?

Jon
 
S

Samuel R. Neff

You don't need to create a class Records to have a typed collection.
Just use List<Record>.

If you feel the need to refer to it by a shortened name, then you can
declare a class:

public class Records : List<Record> { }

And then you'll have a complete fully-functioning type-specific
non-generic collection (although I generally would not recommend doing
this, it's shorter but not as self-documenting).

HTH,

Sam
 
V

Vadym Stetsiak

Hello, Venkatesh!

VB> public void Add(Record objRecord)
VB> {
VB> this.Add(objRecord);
VB> }

Is a infinite-recursive call, that is why you get an exception.

You Records class doesn't have underlying storage for records.
For instance, List<Record>

public class Record<Record> : IList<Record>
{
List<Record> internalList = new List<Record>();
.......
public void Add(Record record)
{
internalList.Add(record);
}
....
}

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com



You wrote on Wed, 12 Sep 2007 05:45:35 -0700:

VB> Hi All,

VB> I am trying to inherit the Generic IList<T> interface to form the
VB> typed collection of objects of type T. Following is my sample code,


VB> Public Class Record {
VB> public string name;
VB> public string address;

VB> Public Record(string sName, string sAddress)
VB> {
VB> this.name = sName;
VB> this.address = sAddress;
VB> }
VB> }

VB> Public Class Records<Record> : IList<Record>
VB> {
VB> public void Add(Record objRecord)
VB> {
VB> this.Add(objRecord);
VB> }

VB> public void Remove(int index)
VB> {
VB> this.RemoveAt(index);
VB> }

VB> ........ with all the explicit member of IList interface }

VB> Implementation:
VB> ------------------------
VB> Records objRecords = new Records();
VB> objRecord.Add(new Record("xyz", "hyd"));


VB> This code compiled fine, but at runtime the this.Add inside the
VB> Records class has thrown StackOverFlowException. I am very new to
VB> using Generics am confused with this error. Please help me
VB> understand if what I am trying to implement is correct or is there
VB> any other way to implement generic custom typed collection of
VB> objects.

VB> Please let me know if you need any other information.
 
M

Marc Gravell

Add() appears to call itself... have you got some kind of backing list
somewhere? The other option is to simply start with Collection<Record>

Also - not sure you need the generic on Records - I'd have something
like:

public class RecordCollection : Collection<Record> {
// job done
}

Marc
 
V

Venkatesh Bhupathi

It's really nothing to do with generics - your implementation of "Add"
just calls itself.
This would fail whether or not you used generics.
What were you expecting to add the entry to?

What do you need to do that List<T> doesn't give you?

Jon

I just want to have typesafe collection of objects so that when I try
to get the objects in the collection, I need not type caste them as in
case of CollectionBase. I am currently implementing as follows by
having an IList inside my Records class,

class EquityRecords : IEnumerable<StructEquityRecord>
{
public bool alternate = true;
public List<StructEquityRecord> equityRecordsList = new
List<StructEquityRecord>();
public void Add(StructEquityRecord objEquityRecord)
{
this.equityRecordsList.Add(objEquityRecord);
}

public void Remove(int index)
{
this.equityRecordsList.RemoveAt(index);
}

public void Clear()
{
this.equityRecordsList.Clear();
}


#region IEnumerable<EquityRecord> Members

public IEnumerator<StructEquityRecord> GetEnumerator()
{
return equityRecordsList.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{ return equityRecordsList.GetEnumerator(); }

#endregion
}


But I want to know is there any better way than this to implement
collection and also particularily using generics? Any sample code
would be of great help.
 
Ad

Advertisements

P

Peter Duniho

Venkatesh said:
[...]
What do you need to do that List<T> doesn't give you?

Jon

I just want to have typesafe collection of objects so that when I try
to get the objects in the collection, I need not type caste them as in
case of CollectionBase.

That's what List<> does for you. If you declare a
List<StructEquityRecord>, then when you retrieve an object from that
collection, it is already typed as StructEquityRecord. No casting required.

So, again...referring back to Jon's question: "What do you need to do
that List<T> doesn't give you?"

So far you haven't described anything that fits that description.

Pete
 
Ad

Advertisements

M

Marc Gravell

I just want to have typesafe collection of objects so that when I try
to get the objects in the collection, I need not type caste them as in
case of CollectionBase.

Collection<T> is a generic equivalent of CollectionBase. List<T> is
similar, but includes a lot of additional functionality that you don't
often expect to see on a collection, although this isn't /necessarily/
bad.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top