X++ "invalid token" message when calling c# library (How to call the method correctly)

1.8k Views Asked by At

I'm trying to create an excel file using x++ code. However, I'm getting the compile error "invalid token" even though intellisense all works correctly when typing out the code. What is the correct way to call the Add() method in x++ for the OfficeOpenXml classes (and in general c# librarys like this?)

using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;

class ExcelTestClass
{
    public static void main(Args _args)
    {
        using (ExcelPackage excel = new ExcelPackage())
        {
            excel.Workbook.Worksheets.Add("Worksheet1");

            
        }
    }
}

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

Of course, just as I give in and ask a question on SA I stumble on the answer while googling.

The correct way to call it is following:

excel.get_Workbook().get_Worksheets().Add("Worksheet1");

From this post: https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/178373/calling-visual-studio-c-classes-in-x/438484 Martin Dráb's answer.

0
On

To add some background detail from Martin Dráb's answer: x++ does not support working with properties of objects in C#/.Net libraries directly. You have to use set and get methods to access those, which is why excel.getWorkbook() works and excel.Workbook does not.

Some other restrictions are

  • you need to use fully qualified names
  • generics are not supported

Additional information can be found at .NET Interop from X++