The question I have is transferring a dotnet DataTable to a TypeScript environment. I have been working with your Alternet Studio for TypeScript project and have added the following code:
scriptRun.ScriptHost.HostItemsConfiguration.AddObject("DataTable", GetDataTable(), Alternet.Common.TypeScript.HostObjects.HostItemOptions.GlobalMembers);
scriptRun.ScriptHost.HostItemsConfiguration.AddObject("MainForm", this); // This exposes some public functions
DataTable is filled by the GetDataTable function
The TypeScript is pretty simple.
var dr = DataTable.Rows;
MainForm.ShowMessage( dr[0].toString() );
I want to be able to iterate through the rows of the DataTable. I have tried to access the first row but dr[0] is always null. It is like TypeScript cannot resolve dynamic results like DataTable.Rows
Also, the word DataTable has a red line under it in the editor as if the object cannot be resolved. Same with the word MainForm. However when the code runs in the debugger and I hover the mouse over it, it says HostItem.
Can you advise?
Hello,
The problem you encountered is related to how calling .NET indexers from JavaScript is implemented in ClearScript library. See the following issue for details: https://github.com/microsoft/ClearScript/issues/132.
Please use the following code to access the DataTable indexer from TypeScript:
var dr = DataTable.Rows;
MainForm.ShowMessage( dr.Item.get(0).toString() );
Also see the following code snippet for various ways to access indexers:
var dt = DataTable;
System.Windows.Forms.MessageBox.Show('dt.Rows.Item.get(0): ' + dt.Rows.Item.get(0));
var dr = dt.Rows.Item.get(0);
// dt.Rows(0) also works, but TS type check error prevents this from running
System.Windows.Forms.MessageBox.Show('dr(0): ' + dr(0));
System.Windows.Forms.MessageBox.Show('dr(\'Name\'): ' + dr('Name'));
System.Windows.Forms.MessageBox.Show('dr.ItemArray[0]: ' + dr.ItemArray[0]);
dr.Item.set('Name', 'HELLO');
System.Windows.Forms.MessageBox.Show('dr(\'Name\'): ' + dr('Name'));
Note that dr.ItemArray[0] works with just square brackets without using the Item property notation. This is because System.Array implements IList, but DataRowCollection and DataRow do not implement it.
In JavaScript, you can also use dt.Rows(0) notation instead of dt.Rows.Item.get(0). But in TypeScript it requires a special type information support, otherwise the script will not compile. We will fix, but for now please use dt.Rows.Item.get(0) notation.
The problem you encountered is related to how calling .NET indexers from JavaScript is implemented in ClearScript library. See the following issue for details: https://github.com/microsoft/ClearScript/issues/132.
Please use the following code to access the DataTable indexer from TypeScript:
var dr = DataTable.Rows;
MainForm.ShowMessage( dr.Item.get(0).toString() );
Also see the following code snippet for various ways to access indexers:
var dt = DataTable;
System.Windows.Forms.MessageBox.Show('dt.Rows.Item.get(0): ' + dt.Rows.Item.get(0));
var dr = dt.Rows.Item.get(0);
// dt.Rows(0) also works, but TS type check error prevents this from running
System.Windows.Forms.MessageBox.Show('dr(0): ' + dr(0));
System.Windows.Forms.MessageBox.Show('dr(\'Name\'): ' + dr('Name'));
System.Windows.Forms.MessageBox.Show('dr.ItemArray[0]: ' + dr.ItemArray[0]);
dr.Item.set('Name', 'HELLO');
System.Windows.Forms.MessageBox.Show('dr(\'Name\'): ' + dr('Name'));
Note that dr.ItemArray[0] works with just square brackets without using the Item property notation. This is because System.Array implements IList, but DataRowCollection and DataRow do not implement it.
In JavaScript, you can also use dt.Rows(0) notation instead of dt.Rows.Item.get(0). But in TypeScript it requires a special type information support, otherwise the script will not compile. We will fix, but for now please use dt.Rows.Item.get(0) notation.