Posted By: ilya | Dec 27th, 2005 @ 8:07 AM
page 1 of 1
Comments: 9 | Views: 4172
ilya
ilya
Mastermind
I was just wondering if anyone knows if it's possible to have indexers in a collection that uses Generics and returns something from the collectionbase that internally holds objects.
The problem that i am having is that when i try to return the object by doing base[index] I get a cast error since I can't cast an object to T

Example:

public class TestList<T>:CollectionBase
{
   public T this[index]
   {
      //this will cause an error
      get{return base[index]}
      //This will also cause an error since as keyword cannot be used
      get{return base[index] as T;}
   }
}

Thanks,

Ilya
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?

You shouldn't use CollectionBase when you're creating a custom generic collection. If you want to create a custom generic collection, it's better to inherit from System.Collections.Generic.ObjectModel.Collection<T>. This also solves your problem.

Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
ilya wrote:
I was just wondering. I am still going to convert my collection using this new Collection<T>.

That's not so, since Collection<T> already uses T as the type, not Object, so there's no casting needed. In fact, you won't need to define an indexer of your own at all like you did above, since Collection<T> already has an indexer that returns T.

Unless I misunderstand what you're trying to do, there should be no problem.
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?

It still isn't really clear what you want to achieve. Could you perhaps post some of the code of one of these existing collections, and explain in greater detail what you want to achieve and how you want to use generics to achieve it?

cravikiran
cravikiran
Ravi Chodavarapu

I have not played around with generics in .NET 2.0 specifically much but it sounds like an explicit cast should work where your return statement is... so

return (T) base[index];

I would imagine it will throw some sort of type casting exception if that conversion cannot be done at runtime.

footballism
footballism
I've been avalonized!
ilya wrote:
cravikiran wrote:

I have not played around with generics in .NET 2.0 specifically much but it sounds like an explicit cast should work where your return statement is... so

return (T) base[index];

I would imagine it will throw some sort of type casting exception if that conversion cannot be done at runtime.



Well thanks for the suggestion.
This line of code had to be modified in my case but it helped me fix my issue.

Since my custom collection would not return System.Object type but my own BaseObject type in which case generics did not like converting back to the <T>(type parameter) that was specified.
So my solution was to cast the object comming from my collection to object and then cast it to the <T> type like this:

return (T)(base[index] as object);

Thanks,

Ilya

This is a very unusual use of generics, please post your code here, and probably we can learn one thing or two from it:p

Sheva