2

I have this .NET 4.0 code:

var myTable = tables[1];
myTable = tables.Item["Table1"];
myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");

I am trying to compile with .NET 3.5 and getting a bunch of errors:

Error 23 Property, indexer, or event 'Item' is not supported by the language; try directly calling accessor method 'Microsoft.Office.Interop.Excel.ListObjects.get_Item(object)'

and

Error 24 'Microsoft.Office.Interop.Excel.ListObjects' does not contain a definition for 'OfType' and the best extension method overload 'System.Linq.Queryable.OfType(System.Linq.IQueryable)' has some invalid arguments

and

Error 25 Instance argument: cannot convert from 'Microsoft.Office.Interop.Excel.ListObjects' to 'System.Linq.IQueryable'

Can you please guide me on exactly what these errors mean so that I can try to convert this to .NET 3.5?

Thank you so much for your advice.

1
  • 6
    There's no simple fix for this. The ListObjects interface is kinda fumbled, it has an Item property that's not the default property of the interface. The default one is named _Default in the MSDN article. C# version 3 and earlier only supports one indexer. The wrong one for ListObjects. You have to explicitly use get_Item(), just as the error message says. Which breaks Linq. Commented Oct 17, 2012 at 19:35

1 Answer 1

2

This question refers to an answer given for This command requires at least two rows of source data; the code is showing 3 different ways of accessing a table/listobject in code.

This was the code I posted:

    var myTable = tables[1];
    var myTable = tables.Item["Table1"];
    var myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");

I meant to illustrate that there are different ways of accessing a table in code.

To put it in context, I have created a Ribbon with a single button called "TestButton". Here is the entire code, should work with 3.5:

using Excel = Microsoft.Office.Interop.Excel;


    private void TestButton_Click(object sender, RibbonControlEventArgs e)
    {
        var worksheet = (Excel.Worksheet) Globals.ThisAddIn.Application.ActiveSheet;
        var tables = worksheet.ListObjects;
        var table = tables.Item["Table1"]; // this is the line you are referring to

        //do something with table
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.