By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,236 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,236 IT Pros & Developers. It's quick & easy.

Getting the correct Type back from ArrayList

P: n/a
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!!!

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

WildHare wrote:
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!!!



Nov 15 '05 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

P: n/a
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!
"Jasper Kent" <ja*********@hotmail.com> wrote in message
news:#o**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4

P: n/a
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/e...eDowncast.html

Hope this helps
Jay
"WildHare" <ki***********@groton.pfizer.com> wrote in message
news:e8****************@TK2MSFTNGP12.phx.gbl... 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!!!


Nov 15 '05 #5

P: n/a
Pick up a copy of GOF to learn all about design patterns, at least some
"standard" ones:
http://www.amazon.com/exec/obidos/tg...85645?v=glance

This web site provides good C# examples for the above book, and decent
explanations that can stand alone.
http://www.dofactory.com/Patterns/Patterns.aspx

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
"WildHare" <ki***********@groton.pfizer.com> wrote in message
news:eT*************@TK2MSFTNGP12.phx.gbl...
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!
"Jasper Kent" <ja*********@hotmail.com> wrote in message
news:#o**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #6

P: n/a
On Sun, 14 Sep 2003 16:35:30 -0400, "WildHare"
<ki***********@groton.pfizer.com> wrote:
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.
--
http://www.kynosarges.de
Nov 15 '05 #7

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.