Category Archives: NAV2017

Create Instance of DotNet variables – the hard way!

Have you ever seen this error:

Assignment is not allowed for this variable? But it’s just a DotNet class that I’m trying to make an instance of. (The class in my example is DocumentFormat.OpenXml.Spreadsheet.Text that is used for adding strings to Excel sheets).

But this is Applied Hacking, so we can use reflection to get around this. Here is “DotNetConstructor”, simplified to a version where the constructor takes just one parameter

The idea is simple, get the ConstructorInfo and Invoke it. To do that, you need a DotNet Array first with the Types of parameters and then antoher array with the parameter values. This function can easily be extended to handle multiple parameters.

So instead of using:

Text := Text.Text('Value');

We can just do:

DotNetConstructor(Text,'Value',Text);

Enjoy 🙂

Let’s get some more return value types

One of the things that I’m always missing in Microsoft Dynamics NAV is support for more return value types:

I would like to be able to return all variables types, to support a more functional programming flow. Currently, I’m missing:

  • Record
  • DotNet
  • Variants
  • RecordRef
  • FieldRef
  • Streams

Hey Microsoft, how about adding some of these 🙂

(I know that I can just use VAR parameters, but they generate a cluttered program flow, one example is that it’s not possible to pass the output of a function call as a parameter to another if the return value is a VAR parameter).