Tech Off Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Generics + Indexers?

Back to Forum: Tech Off
  • User profile image
    ilya

    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

  • User profile image
    Sven Groot

    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.

  • User profile image
    ilya

    Thanks for the info, but do you know if there is a way to return an object back using the type parameter "<T>" in Generics?

    I was just wondering. I am still going to convert my collection using this new Collection<T>.

    Thanks,

    Ilya

  • User profile image
    Sven Groot

    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.

  • User profile image
    ilya

    Well the thing is that I already had implemented custom functionality inside my custom collection class where i can find objects by more than just the "int index" indexer and i I had some other custom code written so I did not want to re-write the code and plus there would be a lot of changes so what i was looking for is to be able to inherit from the base collection that i already have and then be able to use generics to be able to return a specific type based on the Generics Type Parameter.

  • User profile image
    Sven Groot

    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?

  • User profile image
    cravikiran

    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.

  • User profile image
    ilya

    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

  • User profile image
    footballism

    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

  • User profile image
    ilya

    What code do you want to see exactly?

    All I am doing is inheriting from CollectionBase and overriding some functions so i can raise more events and do some pre and post processing and so I can return strong typed objects back.

    This worked fine for me when I needed to return my custom types that i knew about, but in one case i needed to be able to sore and retrieve different objects from the collection so I needed to create a Generic collection class that would take in a type of object that I needed to store it but still be strongly typed and with all the event handlers and everything.
    So what I ended up doing is creating a new class and inheriting from my custom collection class but this time I used Generics "<T>" type parameter on the new "GenericCollection" class to be able to store and retrieve objects but underneath I still has my custom objects so I needed to override the indexers to return the <T> type of objects and also the methods had to take in any type of object.

    If you need something more specific I can probably get a small sample for you.

    Thanks,

    Ilya

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.