Re: iterating over collection

Trey Nash <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <000b01c6d662$c1f49ad0$8100a8c0@tbone>
I'm having trouble wrapping my head around your problem without a short code
example.  :-)

However, I'm going to assume that you are trying to craft some sort of
generic method that can operate on types to be determined at runtime?  If
that is the case, then delegates (and usually generic ones) become your
greatest friend.  That's because you must externalize those operations,
whether they be conversion operations or some other type of operation or
calculation.  After all, the Converter<TInput, TOutput> delegate is defined
in mscorlib to serve this purpose.

Casting to System.Object and back throws away most of the benefits of using
generics in the first place.  If you do that, you may as well just use the
non-generic dictionary object and store everything as System.Object.
However, the efficiency and type-safety of generics would be lost and the
compiler would not be able to catch type errors at compile-time for you.

Hope this helps,

        -Trey

Author of "Accelerated C# 2005"
http://www.amazon.com/Accelerated-C-2005/dp/1590597176


> -----Original Message-----
> From: Discussion relating to the specifics of the C# and
> Managed C++ languages [mailto:[email protected]]
> On Behalf Of Peter Osucha
> Sent: Monday, September 11, 2006 9:44 AM
> To: [email protected]
> Subject: [DOTNET-CX] iterating over collection
>
> I have an abstract base object.
>
> I have several classes that are derived from the base object.
>
> I have several collection classes (one for each of the
> derived classes) that internally use Dictionary<> objects -
> but of course the generic collections use derived objects
> even though all the derived objects are derived from the base object.
>
> I have several (one for each collection class) methods into
> which I pass a collection class instance and which then look
> for a specific object in the collection based on a property
> that comes from the base object.
>
> Is there a way (I can't figure one out and don't know if it's
> possible since the collection classes are all of specific
> object types) to somehow have one method into which I could
> pass any of the collection classes (since the are all based
> on the Dictionary<> object though with different generic
> objects even though all based on the abstract base object)
> and then somehow loop over them after casting to the base object type?
>
> It seems there *ought* to be a way even if I have to do some
> minor rework on the derived objects or the collection objects.
>
> I hope this made some sense.
>
> Peter
>
>
>
> ===================================
> This list is hosted by DevelopMentor.  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.