Getting the correct Type back from ArrayList

  • Thread starter Thread starter WildHare
  • Start date Start date
W

WildHare

If I have a class and I add it to an ArrayList and then want to access that
class using using the index operator (e.g. ArrayList[x]) the ArrayList
returns a type "Object". I can cast the return to the correct type (my
class) but that will lead to very convoluted calls to get embedded elements
or to call methods.

For example:

I have a class called "Field"

I Add Fields to the ArrayList with ArrayList.Add(Field)

I can get a Field back by calling

Field returnfield = (Field)ArrayList[x]; // x is the index returned from
the Add() method above

If Field has a function, for example GetValue() the call to get the value
looks like this:

string TheValueIWant = ((Field)ArrayList[x]).GetValue(); // this works but
is cumbersome!!!!

SO...the question is....

How can I make the return of ArrayList[x] be of type "Field" rather than of
type "Object"?

I hope this is clear!!!
 
The easiest way to do this requires a little overhead, but it's
basically the Decorator design pattern.

Create another class called Fields that implements IEnumerable, and
keeps a private reference to an ArrayList.

Then you can implement an indexer as

public Field this[int index]
{
return (Field) myArrayList[index];
}

You'll also have to implement Add, Remove, Clear, Count, and any other
methods that you'd want to use from the ArrayList, but these will be
trivial to implement.

regards,
ben
 
I'd say this was an example of the Adapter design pattern, but let's not
quibble.

More significant is that Generics, promised in the next release of C#,
will allow automatic generation of type-safe collections, so we won't
have to go to all this trouble.

Regards,

Jasper Kent
 
Adapter vs Decorator Design Pattern??????

Another thing to read about!!! What is the best resource on patterns? Are
they standard or specific to different organizations? Who is the authority
on these things?

I have looked at both the Decorator and the Adapter and I am not yet clear
on which one I am asking about...but will continue to study. Thanks for the
help!
 
WildHare,
In addition to Ben T's comments.

System.Collection.CollectionBase is a base class that wraps an ArrayList for
you. You just need to add type safe Add, Remove, and indexers. It already
implements IEnumerable, ICollection and IList for you.

It has a number of protected overridable methods that all start with "On",
that allow you validate the data coming into the class via the IList
interface.

CollectionBase.ArrayList - not validated via On* methods
CollectionBase.List - validated via On* methods.


BTW:
How can I make the return of ArrayList[x] be of type "Field" rather than of
type "Object"?
Adding a type safe method that encapsulates the downcast is called
Encapsulate Downcast Refactoring:
http://www.refactoring.com/catalog/encapsulateDowncast.html

Hope this helps
Jay


WildHare said:
If I have a class and I add it to an ArrayList and then want to access that
class using using the index operator (e.g. ArrayList[x]) the ArrayList
returns a type "Object". I can cast the return to the correct type (my
class) but that will lead to very convoluted calls to get embedded elements
or to call methods.

For example:

I have a class called "Field"

I Add Fields to the ArrayList with ArrayList.Add(Field)

I can get a Field back by calling

Field returnfield = (Field)ArrayList[x]; // x is the index returned from
the Add() method above

If Field has a function, for example GetValue() the call to get the value
looks like this:

string TheValueIWant = ((Field)ArrayList[x]).GetValue(); // this works but
is cumbersome!!!!

SO...the question is....

How can I make the return of ArrayList[x] be of type "Field" rather than of
type "Object"?

I hope this is clear!!!
 
How can I make the return of ArrayList[x] be of type "Field" rather than of
type "Object"?

By using a strongly typed collection. For that, you have three
choices:

1. Wait for the next version of the .NET Framework which will support
generics, i.e. types that get other types as parameters.

2. Use CollectionBase which is slow and and a lot of work.

3. Use the freeware template generator CodeSmith (available at
http://www.ericjsmith.net/codesmith) which comes with a set of
strongly typed C# collection templates written by yours truly.
 
There's no authority on patterns, but the GoF (Gang of Four - Eric
Gamma. Richard Helm, Ralph Johnson and John Vlissides) is usually the
first port of call.

They're not specific to organizations, but they do tend to get
specialised within organisations and application areas.

In terms of standardization, they're a bit like the English Languge.
There's no authority, but a degree of de facto standardization emerges
because otherwise no one would be able to talk to one another.

Design Patterns are both about good design and the communication of that
design. In terms of writing your code, you only need to know the
structure of the pattern, but to communicate a design to someone else,
it helps to know the name. This isn't always easy, since patterns get
varied, combined and merged.

However, in this case it looks to me like Adapter. Adapter takes a class
that does what you want, but doesn't have the interface you like (in the
sense of method names, parameters etc., not necessarily an inherited
interface). Adapter doesn't add any significant new functionality.

Decorator adds new functionality and adds it incrementally with multiple
decorators. These decorators form up in a chain and perform their
functionality before calling the next decorator in the chain to do its
work.

Regards,

Jasper Kent
 
Back
Top