We help IT Professionals succeed at work.
Get Started

Consumming managed arrays in Javascript

Last Modified: 2012-06-27
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);
	return cameras->ToArray();
// Here is the JScript part
function OnReady(netobject)
    for(i in netobject.Cameras) // this doesnt work!!!

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE