0

I have a Processing program that I update by running this method:

public void createParam (int nWidth, int nHeight, int nNumBalls, float[] nR, float[] nVel, int[] nC)...

I can do this through Javascript pretty easily as long as I don't try to change the last three values:

      var applet = document.getElementById("ballApplet");
      applet.createParam(divw, divh, 6, null, null, null); 

This works perfectly.

However, I'm trying to use C# to accomplish this with non-null values. I'm using this code:

            string command = "var applet = document.getElementById('ballApplet'); ";          
            command = command + "applet.createParam(divw, divh," + numBalls + ", " + r + ", " + vel + ", " + c + ");"; //width, height, num, radius, vel, c
            JCall(command);

num is an int, radius and vel are float arrays, and c is an int array.

JCall is a method I use to add Javascript to a page. It's not important to this problem and it works as intended.

This results in the Javascript error:

Error: syntax error
Source file: http://localhost:63803/Default.aspx
Line: 1227, Column: 214
Source code:
var applet = document.getElementById('ballApplet'); 
applet.createParam(divw, divh,1, System.Single[], System.Single[], System.Int32[]);

It seems like it's passing the type (System.Single[]) rather than the array itself. How do I pass the array to Javascript?

Update: To be a bit more explicit, there's three pieces of code here.

C#, that calls a JavaScript function.

JavaScript, that calls a method in a Processing applet.

Java, a Processing applet that has the "createParam" method.

Here is how the relevent variables are declared in C#:

            int numBalls = eventCount;
            ...
            float[] vel = new float[numBalls];
            ...
            float[] r = new float[numBalls];
            int[] c = new int[numBalls];
Jeremy
  • 5,365
  • 14
  • 51
  • 80
  • Is your C# snippet (`string command = "var applet..."`) contained in your `createParam` method? I'm confused, because it appears that `createParam` is part of some sort of applet, not a C# program. If not, what is the exact type of `numBalls`? (show us its declaration) – Kirk Woll Jul 26 '11 at 21:54
  • 2
    Use json: http://stackoverflow.com/questions/331976/how-do-i-serialize-a-c-anonymous-type-to-a-json-string – Darm Jul 26 '11 at 21:55

2 Answers2

0

Arrays don't convert to strings like you're thinking. They just show the type information, which is why you're seeing System.Single[]. Instead, you'll have to do it yourself using string.Join:

string command = "var applet = document.getElementById('ballApplet'); ";          
command = command + string.Format(
    "applet.createParam(divw, divh, {0}, [{1}], [{2}], [{3}]);",
    numBalls, 
    string.Join(", ", r.Select(x => x.ToString())),
    string.Join(", ", vel.Select(x => x.ToString())),
    string.Join(", ", c.Select(x => x.ToString()))
);
JCall(command);

Notice also I'm surrounding those arrays with [ and ] characters so they are treated as arrays in Javascript.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • r, vel, and c aren't strings but float and int arrays though. How can I use them? Should I just have them be string arrays? – Jeremy Jul 26 '11 at 22:27
  • What do you mean "they aren't strings"? I know they are arrays, that's why I used the `string.Join` method -- to convert an array like this `new[] { 5, 6, 7 }` into the string `5, 6, 7`. Also note I surrounded them with square brackets, so it actually results in `[5, 6, 7]`, which is the Javascript syntax for specifying an array. – Kirk Woll Jul 26 '11 at 22:28
  • To whomever downvoted: this is precisely the answer the OP is looking for. Just because you wouldn't implement it this way doesn't mean it's not the correct answer. – Kirk Woll Jul 27 '11 at 15:36
  • @Kirk Woll don't get ideas- it wasn't me. – Jimmy Hoffa Jul 27 '11 at 18:22
  • @Jimmy, I would never jump to conclusions nor engage in revenge voting anyway. :) – Kirk Woll Jul 27 '11 at 18:42
  • It wasn't me either, Kirk. Your answer is quite good except that you really can't use string join unless the second argument is a string array. – Jeremy Jul 27 '11 at 18:55
  • @Jerremy, misunderstood your point earlier. Just convert them: `r.Select(x => x.ToString())`. I'll update the answer. – Kirk Woll Jul 27 '11 at 21:17
0

I like using the json serializer in .Net http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx just register the clientscript string.concat("var yourArray = ", serializedJsonString); then the yourArray object will be available in the dom

Jimmy Hoffa
  • 5,909
  • 30
  • 53