locked
CS0021: Cannot apply indexing with [] to an expression of type 'object'

    Question

  • Compilation Error is:"CS0021: Cannot apply indexing with [] to an expression of type 'object'".

    How can I solve this problem? I tried to change "[]" into "()", but I got another error "CS0118: System.Web.UI.WebControls.DataGridItem.DataItem' denotes a 'property' where a 'method' was expected"

     

    Line 74:<ASP:Templatecolumn HeaderText="Article Title">
    Line 75:        <itemTemplate>
    Line 76:                 <a href=articleupdate.aspx?articleid=<%#Container.DataItem["articleid"]%>&colid=<%#Container.DataItem["colid"]%>><%#Container.DataItem["subject"]%></a>
    Line 77:        </itemTemplate>
    Line 78:</ASP:Templatecolumn>

    Thursday, June 29, 2006 2:12 PM

Answers

  • <%# (Container.DataItem as MyClass)["articleid"]%>
    Thursday, June 29, 2006 3:15 PM
  • That object does not contain an indexer property, you can't use the [] expression with it.
    Friday, June 30, 2006 3:14 PM

All replies

  • <%# (Container.DataItem as MyClass)["articleid"]%>
    Thursday, June 29, 2006 3:15 PM
  • James,

     

    I am sorry I haven't quite understood you.

    In my library file, the class is "articlecolumn", so I tried to rewrite this line as follows:

    <%# (Container.DataItem as articlecolumn)["articleid"]%>

    This time, the compilation error is :CS0021: Cannot apply indexing with [] to an expression of type 'article.articlecolumn'

     

    How should I do?

    Friday, June 30, 2006 1:12 AM
  • That object does not contain an indexer property, you can't use the [] expression with it.
    Friday, June 30, 2006 3:14 PM
  • Not knowing what exactly a "articlecolumn" object looks like, I really can't answer that.  But my next guess would be:

    <%# (Container.DataItem as articlecolumn).articleid %>

    Wednesday, July 05, 2006 4:35 PM