Create string[,] and object[,] arrays using VisualAPL

General discussions related to APLNext's VisualAPL.

Moderators: Tech Support, phpbb_admin

Create string[,] and object[,] arrays using VisualAPL

Postby joe_blaze » January 18th, 2011, 6:39 am

The attached pdf document and .zip project illustrate the creation of .Net string[,] and object[,] arrays using VisualAPL. The project also illustrates that .Net does not provide a direct cast of object[,] to string[,] even when every element of the object[,] array is a string.
Attachments
VisualAPLDataTypeConversions2.zip
(1.16 MiB) Downloaded 1973 times
VisualAPL DataType Conversions 2.pdf
(264.17 KiB) Downloaded 2100 times
joe_blaze
 
Posts: 384
Joined: February 11th, 2007, 3:09 am
Location: Box 361 Brielle, NJ 08730-0361

Postby Chris McIntosh » January 19th, 2011, 1:56 pm

Hi, Joe,

These examples do clear up a lot of questions I had, but they do not allow me to do what I need.

In this example, S1 is really an array of characters, rather than an array of strings. Variables O4 and S4 are similar to what I need, which is equivalent to a .Net string[,] type.

I need to be able to do one of the following:

(1) Create a .Net string[,] type where each element is a string, rather than a character. I need to be able to put strings, not individual characters, into each element of this array. Specifically, I need to be able to do something of the form:

O4[1,1] = (string)"New String"
O4[1;2] â†
Chris McIntosh
 

Postby Chris McIntosh » January 19th, 2011, 2:01 pm

One other very minor point.

I'm still a little confused on what uses []io indexing, and what uses 0 indexing. I was under the impression that .Net variables used 0 indexing, but APL/Cielo variables used []io indexing, however ...

If I set []io to be one in the examples here, I find that S1, which I would consider a .Net variable is sensitive to []io indexing.

This is a relatively minor point, and more of an inconvenience than anything else. It's easily solved by getting used to programming with a []io of 0, which I'm not used to. I'm just a little curious.

Chris
Chris McIntosh
 

Postby Chris McIntosh » January 19th, 2011, 2:16 pm

Hi, Joe,

I thought I'd try one last series of things.

O4[1,1] = (object)"Testing"
O4[1,1] = (string)"Testing"

Both gave errors of Unable to cast object of type 'VisualCielo.Numeric.DataObjects.cvar' to type 'System.Object[,]'.

So I tried the following:

// MyVar is a string, as GetType() indicates.
// It's not a VisualCielo.Numeric.DataObjects.cvar type.
string MyVar = (string)"Testing"

// None of the following work. Still get errors.
O4[1;1] â†
Chris McIntosh
 

Postby Ajay Askoolum » January 20th, 2011, 8:39 am

".Net does not provide a direct cast of object[,] to string[,] ..."

This statement is true when the operation is attempted in a single (C#) statement. Each element of the multidimensional object array needs to be coerced into the required data type individually i.e element by element.

object[,] obj1 = new object[,]{{"one","two"},{"three","four" "five"}};

Loop through n= 0 to obj1.Rank-1 (gives the number of dimensions) and query obj1.GetUpperBound(n) to get each dimension of the object, create the string array of the same dimension and loop through each plane, convert each element & store in the string array. I agree it is tortuous but if you already know the rank of your arrays, you might be able to code a generic class.

For 1 dimensional objects, there are simpler solution(s).

// Works for 1-Dimensional array
object[] obj1 = new object[] { "one", "two", "three" };

try

string[] str1 = (string[])Array.ConvertAll<object, string>(obj1, element => element.ToString());

or

string[] str4 = (string[]) Array.ConvertAll<object,string>(obj1, delegate (object i) { return i.ToString(); });

(I'm not sure which version of the framework you are using!)
-------------------------------------
[b]"I need to be able to put strings, not individual characters, into each element of this array. Specifically, I need to be able to do something of the form:

O4[1;1] = (string)"New String"
O4[1;2] â†
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Postby Chris McIntosh » January 20th, 2011, 1:38 pm

Hi, Ajay,

Thanks for your help. These are very good suggestions, but unfortunately don't work.

All of them generate Unable to cast object of type 'VisualCielo.Numeric.DataObjects.cvar' to type 'System.Object[,]' errors.

These problems do not seem to occur with 1 dimensional vectors of strings.

Public String[] TestFn(String[] input)
{
// Declare variables.
String MyVar1

// Get something out of input.
// Both work fine. MyVar1 is string. MyVar2 is object of type Cielo.
MyVar1 â†
Chris McIntosh
 

Postby Ajay Askoolum » January 20th, 2011, 4:09 pm

Chris,
I don't think I have the complete picture of what you are trying to do. In general:

- use double quotes to enclose your literals if you want C# to treat them as string, use single quotes when you want C# to treat your literals as char or char[], use the prefix @ (verbatim string) with double quotes if necessary i.e when your string has C# escape characters.
- when passing strings from Visual APL to C#, the strings must be nested i.e enclose (each of the) string element(s); C# will see the arguments as object or object[,] etc. You can cast each element of object to string (can't do it for the whole object[,] etc) in C#. Visual APL does not understand char, if I recall.
- avoid casting or strong typing in Visual APL anything that you want to pass to C#; use strong typing only when you want to call C# functionality from within Visual APL or Cielo. Strong typing is not necessary in Visual APL.
- string or string[,] coming to Visual APL from C# comes across as nested; use disclose where necessary.

I had related issues when I wrote a C# DLL for use with APL+Win and overcame them using the guidelines above and was able to get all the 101 Microsoft samples to work with APL.

It might be easier to resolve issues with your project to hand. If you can, perhaps you can send me your project in a private message together with a note on the issues you are encountering?
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Postby Chris McIntosh » January 20th, 2011, 4:35 pm

Thanks for your help, Ajay,

I have to be missing something very simple. This can't be this complicated.

In it's simplest form, how can I pass a C# variable of type string[,] to APL, and have APL update element [0,0] of that variable, and return the updated version.

If you can give me a working example of that, that is really all I'm looking for. All the complications above are just trying to accomplish that what seems to me to be a pretty trivial task.

To put what I've said in code, here's what I'd like to do.

In C#:

protected void PerformTests()
{
// Create a string array.
string[,] TestVar = new string[,] { { "One", "Two", "Three", }, { "Four", "Five", "Six" } };

// Update it.
string[,] UpdatedVar = AplCode.UpdateStringArray(TestVar);
}

In APL:

public static string[,] UpdateStringArray(string[,] input)
{
// The magic line goes below.
?????????

// Return test.
return input
}

I think I have a workaround (or maybe the real answer?) now.

I was under the impression I couldn't return an APL variable to a C# program, which is why I was going through the many contortions above. The code below seems to work

public static object[,] WantAplVar()
{
// Create a variable.
Test â†
Chris McIntosh
 

Postby Chris McIntosh » January 20th, 2011, 4:40 pm

Hi again, Ajay,

OK, I've read your reply again more carefully, and I think the key to my problem is your statement here - "- avoid casting or strong typing in Visual APL anything that you want to pass to C#"

If I don't strongly type an APL variable I want to pass to C#, how do I declare it in the receiving C# program? As object[,] in C#? Or could I reference it as string[,] in C#? If you can tell me that, I suspect my problems might be solved.

Chris
Chris McIntosh
 

Postby Ajay Askoolum » January 20th, 2011, 4:47 pm

The 101 samples write up is here http://www.scribd.com/doc/19525586/Linq ... -to-AplWin

In particular, look at the examples using strings or string arrays.

I think one of the changes in the latest release of Visual APL is to include handling for data type decimal - if you can demonstrate where the latest release breaks compatibility with another version, I am sure Joe will be willing to investigate.

I will shortly be looking at your requirement. In reply to your specific questions:

For literal arguments between C# and Visual APL:
- Do not use strong typing in Visual APL.
- Enclose each literal or element if using arrays.
- In C#, use object or object[,]. Cast each element to string, if/where necessary.
- In Visual APL, use disclose, if/where necessary.

For other types, (e.g int) strong typing does not create problems.
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Postby Chris McIntosh » January 20th, 2011, 5:07 pm

Ajay, this is tremendously helpful. Thank you. I'll look at this in detail.

There has been a break in compatibility. As I'm just getting started with VisualAPL, and don't have an extensive library of code, this won't mean any rewriting for me. It may affect others more.

I'll be very interested to see if you can find the "magic line" for me. Thanks very much for looking into this.

Chris
Chris McIntosh
 

Postby Ajay Askoolum » January 22nd, 2011, 11:53 am

Chris,

The attached project contains working code; you should be able to get a few answers (including that to your specific question, above) from here.

- There is a lot of subtleties involved here i.e. in going from an implicitly typed environment (Visual APL) to a strongly typed environment (C#) and back. Unfortunately, I do not have time to write the prose to explain all of it but most of it should be self explanatory.

The project contains a C# console application that uses a Visual APL .NET class. Step through the console application using F11 and examine the variable created on each line (using right-click & QuickWatch in Visual Studio).

The project illustrates

- how to pass values/variables to/from each environment to the other.

- how to use casting and (function) signatures to move from C# to Visual APL and vice versa.

I discovered that the APL+Win COM interface does a lot of (wonderful) stuff in the background when talking to C#. With Visual APL (all managed code environment and no COM interface), you have to do a lot of the stuff yourself.

- The project is for exploration only & not meant to be any kind of prototype or template for a real application. However, some of the techniques will be useful when constructing a (managed code) Visual APL .NET DLL for consumption by C# or another native .NET language.

- For a real application, I would expect that you will be using Visual APL to deliver the business tier from a C# front end. Here, I assume that a simple call to a startup/initial function in the Visual APL class will be all that you will need to start the processes delivered within the Visual APL class. That is, there won't be a lot of C# to Visual APL (and vice versa) switches albeit this is easily possible (as the project demonstrates).

Please confirm that you are able to open the project (I have used the latest version of Visual APL & Visual Studio 2008 Team Edition) and, in due course, that it all makes sense.

By all means, ask if any aspect of the code is unclear/mysterious.

PS. In a real application, there would be times when you would need to convert variables from one type to another in C# (e.g. from long to int or object to string etc): as Joe has pointed out, there are limits to what can be done but as ALWAYS there are workarounds - I have not covered this in the project.
Attachments
APLtoCS.zip
(1.77 MiB) Downloaded 2085 times
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Postby Chris McIntosh » January 25th, 2011, 2:56 pm

Hi, Ajay,

Sorry for my delay in replying. I've been down with the flu the last couple of days.

I've taken a look at this, and it is very helpful. It didn't do exactly what I wanted, which was take in an array, modify it, and return a modified array. But with what you sent it was trivial for me to figure this out. Code is below.

public Æ’ Modify2(nestedArray,row,column,repl)
{
Z â†
Chris McIntosh
 

Postby Chris McIntosh » January 25th, 2011, 3:08 pm

Hi, Ajay,

I tried one more thing. Taking out the {each} and {enclose} and just using 2 6â
Chris McIntosh
 

Postby Ajay Askoolum » January 25th, 2011, 3:53 pm

With

string SingleWord = philosophy[0, 0].ToString();

I think you need

string SingleWord = {Alt + E) philosophy[0, 0].ToString();

or

string SingleWord = {Alt + E) philosophy[0, 0]

or perhaps

string SingleWord = (string) {Alt + E) philosophy[0, 0]

{Alt + E) are Visual APL keystrokes.

I did mention that you need to treat the project as a series of tips not a prototype for an application!

Let me summarise as follows:

1. C# and Visual APL communicate with objects.
2. C# objects track the type of value it holds (string, int etc)
3. By default, Visual APL objects track the type of value it holds (numeric, literal) as cvar: C# does not understand cvar.
4. To interchange data, when creating objects with Visual APL, use either casting, or function signatures to track values such that C# can understand them as string, int etc.

Chris, I hope you feel better!

Question for Joe: Is there a reference that can be added to a C# project whereby C# can understand cvar? That is, allow C# to map cvar type to a C# type?
Ajay Askoolum
 
Posts: 884
Joined: February 22nd, 2007, 2:16 am
Location: United Kingdom

Next

Return to VisualAPL

Who is online

Users browsing this forum: No registered users and 1 guest

cron