Link to home
Start Free TrialLog in
Avatar of SunnyDark
SunnyDarkFlag for Israel

asked on

Consumming managed arrays in Javascript

Hi,
I have a managed class written in c++.
An instance of the class is passed to a JavaScript function in HTML page via COM,
A COM wrapper for the class is created via Marshal::GetIDispatchForObject.

All the simple properties and methods work correctly, but the properties that return Arrays or List<> are seen in JavaScript debug as "Array of object".
Problem is I cant access any of the objects in the array.
I tried the naive approach : array_var[index]
No go...
I tried wrapping the array in VBArray object... No go...
Any help is appreciated.
Some sample code is below:

// Managed class definition
public ref class ManagedClass
{
    // This is used to create a COM object that is later passed to JScript
    IntPtr AsCOMObject()
    {
	return Marshal::GetIDispatchForObject(this);
    }
    // This is a sample array property
   property array<Camera^>^ Cameras {
      array<Camera^>^ get() {
	List<Camera^>^ cameras = gcnew List<Camera^>();
	for (map<wstring, camera>::iterator iter = _internal->Cameras.begin(); iter != _internal->Cameras.end(); iter++)
	{
	Camera^ oCam = gcnew Camera(&iter->second);
	cameras->Add(oCam);
	}
	return cameras->ToArray();
   }
}
 
 
// Here is the JScript part
function OnReady(netobject)
{
    for(i in netobject.Cameras) // this doesnt work!!!
    {
        alert(i);
        alert(scene.Cameras[i]);
    }
}

Open in new window

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Avatar of SunnyDark

ASKER

This is not what I Am looking for.
Of course I could export the whole strusture of the object as JSON but than:
1. I would need to implement custom serialization to and from JSON.
2. I would loose the bi directional interface , i.e, in order to set some property on the object I would have to come up with a way of setting it on JSON object and than copy it back to the managed client...
There has to be a simplier way...
What does

  for(i in netobject) alert(i+':'+netobject[i])

tell you?
It doesn't enter the loop at all...
 
Then I am out of ideas (I am a js person, not c#)
ASKER CERTIFIED SOLUTION
Avatar of SunnyDark
SunnyDark
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial