473,326 Members | 2,090 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,326 software developers and data experts.

Writing a set extending ArrayList

Sam
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;
}
}

Nov 16 '05 #1
8 6663
the line res = this.Add(value); keeps calling your Add method recursively.

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

"Sam" <an*******@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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;
}
}

Nov 16 '05 #2
Sam wrote:
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.
Nov 16 '05 #3
Sam
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?
Nov 16 '05 #4
Sam <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Sam
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?

Nov 16 '05 #6
Sam <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Sam
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

Nov 16 '05 #8
Sam <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

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

Similar topics

3
by: Glenn Venzke | last post by:
I'd like to write a windows service. Unfortunately, my company would only spring for the standard edition of VB.NET which doesn't support the creation of windows services. Is it possible to write one...
3
by: Stephen | last post by:
I've been working on some code which will hopefully allow me to transfer the contents of an arraylist on one page over to another page. The Arraylist is held in the viewstate object as it is...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
5
by: Stimp | last post by:
I'm populating a dropdown list with non-consecutive values (well the last 3 values are non-consecutive anyway).. What's a shorter way of writing the following?... ddLetMaxPrice.Items.Insert(0,...
20
by: Keith Elder | last post by:
I ran into a unique situation today whereby I have a core library that uses generics to return users from Active Directory. Example: List<ADUser> users = ADUser.GetByName("First", "Last"); ...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
3
by: katis | last post by:
Hi all :) Is it posible in xsd to change only minOccurs value of element in complex type by extending this type? The problem I'm trying to solve is this: I have two complex types -...
59
by: riva | last post by:
I am developing a compression program. Is there any way to write a data to file in the form of bits, like write bit 0 then bit 1 and then bit 1 and so on ....
2
by: Jeff Gaines | last post by:
I am in the process of upgrading to XP 64. VS2008 is running fine but one of my apps, which adds functionality to the Explorer context menu, just won't work under XP 64. Googling resulted in my...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.