How to instantiate an object of .NET class, with List on C/AL?

How to instantiate an object of VATEntry class, with Stock List on C/AL?

Please see below picture.

I want to put values from NAV into JSON string using below class. But How to put values into List?

replace the List by an Array:

public Stock[] stocks {get; set; }

Thanks Jonathan,

But how can I use it on C/AL?

I want to put values into Stock of VATEntry

Regards,

Undy

please publish your c/al code.

Dear Jonathan,

Here are my C/AL code,

VAR

Bridge@1000 : DotNet “‘BridgePosAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3b00c861d26887ba’.BridgePosAPI.Class1”;

BridgeVAT@1001 : DotNet “‘ClassBridge, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8bc2fa84bf3be36f’.ClassBridge.VATEntry” RUNONCLIENT;

BridgeStock@1002 : DotNet “‘ClassBridge, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8bc2fa84bf3be36f’.ClassBridge.Stock” RUNONCLIENT;

StockList@1004 : DotNet “‘mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.Collections.Generic.List`1”;

JsonConvert@1003 : DotNet “‘Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’.Newtonsoft.Json.JsonConvert” RUNONCLIENT;

MyString@1005 : Text;

element@1006 : DotNet “‘mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.Object”;

OnOpenPage=BEGIN

BridgeVAT:=BridgeVAT.VATEntry;

BridgeVAT.amount:=‘300.00’;

BridgeVAT.cashAmount:=‘100.00’;

BridgeVAT.nonCashAmount:=‘200.00’;

BridgeVAT.vat:=‘333.56’;

BridgeVAT.stocks:=StockList; //here must be instantiate a BridgeStock list

BridgeStock:=BridgeStock.Stock;

BridgeStock.code:=‘code1’;

BridgeStock.name:=‘name1’;

BridgeStock.measureUnit:=‘unit’;

BridgeStock.qty:=‘5’;

StockList:=StockList.List();

StockList.Add(BridgeStock.code);

StockList.Add(BridgeStock.name);

StockList.Add(BridgeStock.measureUnit);

StockList.Add(BridgeStock.qty);

FOREACH element IN StockList DO

BEGIN

BridgeVAT.stocks:=element; //here must be instantiate a BridgeStock list

END;

MyString:=JsonConvert.SerializeObject(BridgeVAT);

MESSAGE(’%1’,Bridge.put(MyString));

END;

When I run this page, it appears below error message.

8103.error.png

How can I instantiate BridgeStock list into object of class?

Best Regards,

Undy

Did you solve this issue? I’m facing the very similar issue now.

First try with one record alone… If it fails, enable the debugger and it will tell you what line is wrong.

Also notice can only work in Windows client (dotNET)

When I try to copy a List (DList) into a List variable (WSData.Details) under a different class I get the same error for all these 3 ways ( seperatley) :

  1. DList.CopyTo(WSData.Details)
  2. WSData.Details := DList
  3. WSData.Details(DList)

Compile Error? Windows Client error? Webclient error?

Windows Client error , the similar to issue on this page when I run te codeunit I get a simillar error

8103.error.png

I know that this is an old thread, however finding a solution to my own similar issue took some time, hence sharing in case so also needs this.

I think this works:

// Stocks: dotnet-array  : System.Array.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
// Type: dotnet - type:  System.Type.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

BridgeVAT:=BridgeVAT.VATEntry;
 BridgeVAT.amount:='300.00';
 BridgeVAT.cashAmount:='100.00';
 BridgeVAT.nonCashAmount:='200.00';
 BridgeVAT.vat:='333.56';

//1 Create an array of stock

BridgeStock:=BridgeStock.Stock;
Stocks := Stocks.CreateInstance(GETDOTNETTYPE(BridgeStock),1);

//2- and for all Stock object:
// a- assign values of Stock:

 BridgeStock:=BridgeStock.Stock;
 BridgeStock.code:='code1';
 BridgeStock.name:='name1';
 BridgeStock.measureUnit:='unit';
 BridgeStock.qty:='5';

//b - and add it to array :
Stocks.SetValue(BridgeStock,0);

// 3-then assign this array into stocks property of BridgeVAT 

Type := GETDOTNETTYPE(BridgeVAT);
Type.GetProperty('stocks').SetValue(BridgeVAT,Stocks);