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

.NET ProgrammingScripting Languages

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
Michel Plungjan

ASKER
SunnyDark

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...
Michel Plungjan

What does

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

tell you?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
SunnyDark

It doesn't enter the loop at all...
 
Michel Plungjan

Then I am out of ideas (I am a js person, not c#)
ASKER CERTIFIED SOLUTION
SunnyDark

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michel Plungjan

Great.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.