none
Generic property indexer

    Question

  •  

    Hi,

     

    I've googled, windowslivesearched, Yahoosearched etc on Generic property indexer and I found nothing.

    I did try many things with no success to date.

     

    I would need a GENERIC PROPERTY INDEXER.  I explain :

     

    I got a class with an indexer like this

     

     

    public Object this[object ColumnName] // Indexer property to access the fields of a DataTable

    {

    get

    {

    RETURN THE DATA TYPE of the value WITH the value of course

    so instead of CASTING the returned OBJET as this indexer is object, I would just use generic so it really gives me the real DATA TYPE with its value ???

    }

    set

    {

    Save the value to the table... Smile

    }

    }

     

    I thought that would work but it doesn't

    public this<T>[object ColumnName]

     

    So I would just do this

     

    DateTime dtDate = MyClass.Field<DateTime>["TheColumnI'mlookingFor"];

     

    instead of

     

    DateTime dtDate = (DateTime)MyClass.Field["TheColumnI'mlookingFor"];

     

    Thank you for any help on this.

    Wednesday, February 06, 2008 3:44 PM

Answers

  • A workaround is to declare a method (cannot use the "this" accessor) like the following.

     

    public T GetValue<T>(object ColumnName)

    {

    return (T) yourValue;

    }

     

    You could call itas follows:

     

    DateTime dtDate = MyClass.GetValue<DateTime>("TheColumnI'mLookingFor")

     

    So, you were on the right track, you just can't use generics this way with the "this" keyword.

    Thursday, February 07, 2008 12:08 AM