Am I crazy or...

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I don't understand why the ArrayList's Add() method is public virtual, but
the array of items that the ArrayList uses is private (the variable is
'object[] _items' in the ArrayList). This makes it impossible to re-write
the Add() method in a class that derives from ArrayList. I have this type
of problem with so many of the CLR classes. Why bother making the method
virtual if it's impossible to re-write it because it uses private variables.
Am I making any sense? I guess I'll just have to re-write the entire
ArrayList class just so i can make a small change to one method.

Chris
 
Chris said:
I don't understand why the ArrayList's Add() method is public
virtual, but the array of items that the ArrayList uses is private
(the variable is 'object[] _items' in the ArrayList). This makes it
impossible to re-write the Add() method in a class that derives from
ArrayList. I have this type of problem with so many of the CLR
classes. Why bother making the method virtual if it's impossible to
re-write it because it uses private variables. Am I making any sense?
I guess I'll just have to re-write the entire ArrayList class just so
i can make a small change to one method.

If you want to make changes, chances are you'd be better off with
CollectionBase rather than ArrayList. What change do you want to make?
 
If you want to make changes, chances are you'd be better off with
CollectionBase rather than ArrayList. What change do you want to make?

You're absolutely right. I did that soon after I posted. I was just
getting too frustrated with the ArrayList that I think to look somewhere
else.

just in case you're curious, I wanted to keep the Add() method from just
appending on the end every time, it will search for the first null value in
the array and assign the value to that index instead.

P.S. you helped me with a compression class a while ago (you named it
SpaceSaver). I thought you might want to know that we put that in our
product and it works great. It compresses the files to about 35% of the
original size (on average).

Thanks Jon
 
Chris said:
You're absolutely right. I did that soon after I posted. I was just
getting too frustrated with the ArrayList that I think to look somewhere
else.

just in case you're curious, I wanted to keep the Add() method from just
appending on the end every time, it will search for the first null value in
the array and assign the value to that index instead.

You could do that easily anyway:

public override void Add(object o)
{
for (int i=0; i < Count; i++)
{
if (this==null)
{
this = o;
return;
}
}
// No nulls found
base.Add(o);
}
P.S. you helped me with a compression class a while ago (you named it
SpaceSaver). I thought you might want to know that we put that in our
product and it works great. It compresses the files to about 35% of the
original size (on average).

Hmm... don't even remember that now - but I'm glad it works :)
 
I may well be wrong, but I think the ability to override the Add and other
methods in ArrayList was intended for creating type-safe array lists, not to
really change the way it functions. Again, I may be wrong, but that's the
impression I get.

Anyway, it looks like you got it worked out anyway.

Pete
 
I may well be wrong, but I think the ability to override the Add and other
methods in ArrayList was intended for creating type-safe array lists, not to
really change the way it functions. Again, I may be wrong, but that's the
impression I get.

Actually, that's what CollectionBase is for. I can't think of any
particularly good reasons to subclass ArrayList - but then I tend to
think that composition is more useful than inheritance anyway.
 
What is the problem? You can still use the ArrayList's indexer to access all
elements directly,
you do not need to mess with the internal data structure directly.
 
What is the problem? You can still use the ArrayList's indexer to access
all
elements directly,
you do not need to mess with the internal data structure directly.

the problem *was* that I couldn't access the full capacity of the array.
when I do a new ArrayList(), the object[] array has 16 elements. I was
trying to access all of them, but the indexer on the ArrayList will throw an
error if you try to access an index >= the Count.

here was my problem.. I was just using the wrong property name. this is the
wrong way:

public override void Add(object o)
{
for (int i=0; i<Capacity; i++) {
if (this==null) {
this = o;
return;
}
}
base.Add(o);
}

as Jon said in a post above, this is the correct way:
public override void Add(object o)
{
for (int i=0; i<Count; i++) {
if (this==null) {
this = o;
return;
}
}
base.Add(o);
}

pretty stupid mistake, but what can I say? it was 3:00 in the morning ;)

Chris
 
Jon Skeet said:
Actually, that's what CollectionBase is for. I can't think of any
particularly good reasons to subclass ArrayList - but then I tend to
think that composition is more useful than inheritance anyway.
Minmial reasoning for subclassing ArrayList is pretty much just for a
synchronized version(be it useful or not,ArrayList.Syncronized returns an
ArrayList if its passed an ArrayList). ArrayList has alot of functionality
that, IIRC, CollectionBase doesn't, like ToArray, Sort, BinarySearch etc. It
wouldn't be to terribly hard to define a CollectionBase that provides
these(as you said, composition is generally a better idea) however you still
have to define the methods.

Also CollectionBase doesn't deal with the circumstance of an idiot API that
requires ArrayList instead of IList or ICollection(I've seen 'em, sadly).
 
Back
Top