0

I built a class library application-dll in C# with the class definition below:

namespace Microsoft.Dynamics.NAV.NAVInteropHelper
{
   public class WrapDecimalConvert
   {
      public static Type GetTypeofDouble()
      {
        return typeof(decimal);
      }
   }
}

I added the dll to the application, declared the following variable in my globals

Name      DataType    Subtype   Length
varArray  DotNet    System.Array.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
varDotNet DotNet    Microsoft.Dynamics.Nav.NavInteropHelper.WrapDecimalConvert.'NavInteropHelper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'    

Then in my code, I have the following

arraySize := 10; 
// Creates an instance of the .NET Framework array that contains the decimal type.  
varArray := varArray.CreateInstance(GETDOTNETTYPE(varDotNet.WrapDecimalConvert()), arraySize);
// Clears the object instance because it is no longer used.  
CLEAR(varDotNet); 
// Sets the data in the array.
FOR i := 0 TO (arraySize -1) DO
varArray.SetValue(i+100.0,i);

Everything seems to be fine but upon calling the function, I got this error

A call to Microsoft.Dynamics.Nav.Runtime.NavDotNet[].SetValue failed with this message: Object cannot be stored in an array of this type.

I've been stucked on this for days. Any help will greatly appreciated.

hopeforall
  • 401
  • 1
  • 6
  • 20

2 Answers2

0

Assume for some reason Dimensions is not suitable for you.

Everything Nav is dealing with is wrapped internally in Object class. So when you writing varArray.SetValue(i+100.0,i); the i+100.0 is not Decimal, it is Object. You can try to define variable of type dotnet subtype Decimal in Nav and assign value to it, then pass this variable to array.

dec := i+100.0;
varArray.SetValue(dec, i);

Also, I think, you can get rid of the type wrapper you have in external library by using the following.

Tp  DotNet  System.Type.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Tp := Tp.GetType('System.Decimal, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089');

Not quite sure about semantics inside the GetType though.

Mak Sim
  • 2,148
  • 19
  • 30
0

WIth @Mak Sim's input, I replaced

// Creates an instance of the .NET Framework array that contains the decimal type.  
varArray := varArray.CreateInstance(GETDOTNETTYPE(varDotNet.WrapDecimalConvert()), arraySize);

with

 // Creates an instance of the .NET Framework array that contains the decimal type.  
 varArray := varArray.CreateInstance(GETDOTNETTYPE(initDec), arraySize);
 // where initDec is decimal variable

With that, I was able to store values in the array.

hopeforall
  • 401
  • 1
  • 6
  • 20