by joe_blaze » October 23rd, 2013, 5:14 pm
Hi Ajay:
Thanks for your examples, I'm sure that others will learn from them too.
Let's examine your specific example:
I recommend that you assign (,[⍬]'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23"); ') to an APL+WIn variable and check its shape, which is 90 1.
Problems with your use of the CSE 'Exec' method:
Since your argument is an APL+Win text array with 90 rows and only one column, it cannot represent a valid C# executable statement.
Having only one column means that the C# keywords, e.g. "String" "Format" are split on different rows and that is not valid C#.
Being a 2-dimensional APL+Win text array your argument is a valid argument to the []CSE 'Exec' method so APL+Win does not throw an exception
However since your argument does not represent valid C# executable statements, the Microsoft C# debugger/compiler does throw an exception
The APL+Win []CSE system function indicates this with the -1 result.
When you receive a -1 result, you should use the CSE 'GetLastError' method to see why the Microsoft C# debugger/compiler is complaining,
If you do that you will see that the C# exception message is '... ; expected'. That is a reasonable error when a key word is split.
Problems with your use of the CSE 'ExecStmt' method
Since your argument a APL+Win text array (with 90 rows and 1 column) and not a text vector, it is not a conformable argument to the CSE 'ExecStmt' method.
The 'ExecStmt' method requires an APL+Win text vector argument.
In this situation, the APL+Win []CSE system function throws an APL+Win exception.
Now let's try to get the CSE to do what you want.
The CSE 'Exec', 'ExecStmt' and 'ExecFile' methods are not designed to return values to APL+Win from the associated C# instance of the CSE.
These methods act upon the C# instance of the CSE and report the efficacy of the C# executable statements in their arguments.
To obtain values of C# variables, properties or method results, use the CSE 'GetValue' method.
So to modify your example a bit:
⎕cself←'C'⎕cse 'Init' 'System'
⎕cse 'ExecStmt' 'using System;'
0
⎕pw←120
S←'String.Format(@"{0} this is my {1} string {2} recycle {0} ","ajay", "new","900.23");'
⎕cse 'ExecStmt' S
0
⍝ ↑Executed correctly in C#, but nothing was assigned on the C# side
⍝ So using 'ExecStmt' is not going to do what you want.
⎕cse 'GetValue' S
ajay this is my new string 900.23 recycle ajay
⍝ ↑Using the CSE 'GetValue' method does the job!