views:

353

answers:

2

What is the best way to convert a JavaScript array of doubles

return [2.145, 1.111, 7.893];

into a .NET array of doubles, when the Javascript array is returned from a webbrowser controls document object

object o = Document.InvokeScript("getMapPanelRegion");

without using strings and parsing?

The object returned is of type __ComObject as it is an array being returned. The array will be a fixed size as I am using this to return three values back to the calling code. My current solution is to return a | deliminated string of the three value, I then split and parse the string to get my three doubles. If possible I would like to not have to use the string manipulation as this feels like a hack.

+1  A: 

From MSDN (HtmlDocument.InvokeScript Method):

The underlying type of the object returned by InvokeScript will vary. If the called Active Scripting function returns scalar data, such as a string or an integer, it will be returned as a string ...

It seems that integers will be returned as string, you could asume that the same applies to doubles.

EDIT: I forgot that you were talking about an array. Anyhow, MSDN continues:

... If it returns a script-based object, such as an object created using JScript or VBScript's new operator, it will be of type Object ...

So it seems that you will get an object instead, but that doesn't help the fact that you will have to convert it inside your C# code.

EDIT 2:

As for the conversion of the actual ComObject. After reading this which is about Excel, but it still returns a ComObject, it seems that your current aproach with the string seems like the simpler one. Sometimes a hack is the best solution :D

Charlie boy
But I have an array of doubles and they are treated differently.
Stevo3000
Sorry! I noticed. I've updated my answer
Charlie boy
The problem is getting the __ComObject into my .NET array.
Stevo3000
I've updated again
Charlie boy
I agree that a hack is sometimes the best solution, but I'll feel better about it once I've explored as many possibilities as I can.
Stevo3000
A: 

Can we talk architecture first? My I ask why webbrowser control? We have stopped using it for exactly this kind of reasons. It is usually more pain than gain. It is also (and usually) product of an tightly coupled architecture. For local html pages (aka clients) we use hta. If (and that does not happen often) we need to have C# code in the "back" we use our simple C#/COM+ enabled local server. (note: this is simpler than HttpListener based local server ).

Second: implementation issues.

Imagine you are a developer of Document.InvokeScript(); . What would you return from it? What type? What you get from the HTML Document is DOM. Script is kind of a DOM node. And javascript/DOM is implemented as "com object" indeed. which has IDispatch interface, which in turn returns VARIANT's to the host (aka browser). For a simple reason that javascript has no types in the C and COM meaning. The bottom line is: javascript is seen by COM as returning (and receiving) javascript Object's only. What actually is inside is implementation dependent. Therefore it would be extremely difficult to guess what is inside IDispatch instance returned by javascript and to parse this into the COM types. This is left to the users/callers.

So. It is much easier to establish strings as the only type used to communicate with the "back" from javascript. And to structure these strings as either xml or json. Exactly the same as communicating with distant web services, we communicate with the local C#/COM+ server.

// HTA,  non Ajax solution
var local_server = new ActiveXObject("Stevo3000.LocalServer");
local_server.send(
   "{ method: 'getname', id: 123 }" , 
function ( data ) 
{
   alert("Stevo says:" + data ) ;
}
) ;

NOTE: Since we are talking here C#, we know that all of this is happening on the WIN32 desktop, so we use MSFT tools and technologies. Exactly the same principle can be applied for cross browser, platfom agnostic solution, by using AJAX only.

// Pure Ajax
// using jQuery
$.post("http://stevo3000.com/nameprovider.aspx", 
   { method: "getname", id: "123" },
  function(data){
       alert("Stevo says:" + data ) ;
  });

Hope this helps?

--DBJ

DBJDBJ
We are using a webbrowser control as we are showing google earth on our form in the webbrowser control. The javascript returns the lat, long and altitude that the user has navigated the control to. The architecture will not be changed, which is why I'm asking this question.
Stevo3000