Writing a set extending ArrayList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m trying to write a Set class where it’s a list that you can add an element only once. So I naturally tried to extend the ArrayList and wrote this code. The problem is that it compiles but during execution it gives this error and points to the “if (!(this.Contains(value)))†line :
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

Here is my program:
public class Set : System.Collections.ArrayList
{
public Set()
{

}

public override int Add(object value)
{
int res;
if (!(this.Contains(value)))
{
res = this.Add(value);
}
else
{
res = this.IndexOf(value);
}
return res;
}
}
 
the line res = this.Add(value); keeps calling your Add method recursively.

change the line to res = base.Add(value);

Sam said:
I'm trying to write a Set class where it's a list that you can add an
element only once. So I naturally tried to extend the ArrayList and wrote
this code. The problem is that it compiles but during execution it gives
this error and points to the "if (!(this.Contains(value)))" line :
 
Sam said:
public override int Add(object value)
{
int res;
if (!(this.Contains(value)))
{
res = this.Add(value);

You need to use base.Add here, otherwise you're calling right back into
this Add method and will do so until the stack runs out of space.
 
Thanks. I’m coming from Java and was wondering about the ToString method. In Java whenever I wanted to check my collections, I could easily call its toString method and it would show the contents. In C# it seems they have not implemented a default ToString for each collection, right? Do I have to extend all the collections I need and override their ToString method?
 
Sam said:
Thanks. I?m coming from Java and was wondering about the ToString
method. In Java whenever I wanted to check my collections, I could
easily call its toString method and it would show the contents. In C#
it seems they have not implemented a default ToString for each
collection, right? Do I have to extend all the collections I need and
override their ToString method?

I wouldn't bother doing that, myself. In the debugger you can just
expand them, and in code you can easily write a utilty method which
concatenates the result of calling ToString on each element.
 
I have also written a List class that extends ArrayList. How is it possible to convert a String [] to a List, or ArrayList elegantly? I know that you can do the reverse. I tired
List res = (List) List.Adapter((System.Collections.IList) tokenList.GetEnumerator());
tokenList is a String[]

It compiles but gives a runtime error. I can iterate throw the elements of the Array and add them to a new List but is it the only way?
 
Sam said:
I have also written a List class that extends ArrayList. How is it
possible to convert a String [] to a List, or ArrayList elegantly? I
know that you can do the reverse. I tired List res = (List)
List.Adapter((System.Collections.IList) tokenList.GetEnumerator());
tokenList is a String[]

It compiles but gives a runtime error. I can iterate throw the
elements of the Array and add them to a new List but is it the only
way?

Arrays implement ICollection, so you can just use the constructor to
ArrayList which takes an ICollection parameter, assuming I understand
your question correctly.
 
Thanks! It didn’t work because I didn’t use this constructor in my new class
public List(System.Collections.ICollection c) : base(c

I’m now considering creating strongly typed Lists and Sets
I have Set that extends List and TextList that extends List
List <- Se
List <- TextLis

Now imagine if I want to create a TextSet. Should I extend Set or TextList? Unfortunately multiple inheritance is not allowed
List <- Set <- TextSe
o
List <- TextList <- TextSe
 
Sam said:
Thanks! It didn?t work because I didn?t use this constructor in my new class.
public List(System.Collections.ICollection c) : base(c)

I?m now considering creating strongly typed Lists and Sets.
I have Set that extends List and TextList that extends List:
List <- Set
List <- TextList

Now imagine if I want to create a TextSet. Should I extend Set or
TextList? Unfortunately multiple inheritance is not allowed!
List <- Set <- TextSet
or
List <- TextList <- TextSet

And that's where you want to consider defining an ISet interface, and
make your class implement both IList and ISet - the actual
implementation derivation shouldn't matter too much.
 
Back
Top