none
Best way to expose jagged arrays from C# classes to legacy COM clients (VB6) RRS feed

  • Question

  • I have a C# class which contains a jagged array as one of its properties exposed to COM:

    string[][] Measurements

    The property vcan easily be read/set by .Net applications but I was asked to expose it to COM applications (namely VB6 and VBScript).  I was able to expose all the properties to COM except this jagged array because there's no marshalling support for jagged arrays.  So, I've tried to change it from string[][] to string[,] Measurements, but now I can see this property in VB6 and when I call it I can't get the data populated in a VB array.  LBound and UBound give the correct array size but this code give wrong type error:

        For r = LBound(objClass.Measurements, 1) To UBound(objClass.Measurements, 1)

            For f = LBound(objClass.Measurements, 2) To UBound(objClass.Measurements, 2)

               sMeasurements = sMeasurements & objClass.Measurements[r, f])
            Next
        Next
        
    And this code returns the data scrambled: the values in objClass.Measurements(0,0) then objCard.Measurements(1,0) then (0,1) then (1,1) etc. instead of (0,0) then (0,1) then (0,2) then (0,3) etc.:

        For Each sReading In objClass.Measurements
                Debug.Print sReading
        Next

    Any idea how I should declare the jagged array in the C# module and how I should call it from VB6 or VBScript?

    Thanks,

    Martha
    Monday, March 23, 2009 11:13 PM

Answers

  • int r = 0; 
    While (r < UBound(objClass.Measurements, 1))

        sMeasurements = sMeasurements & objClass.Measurements[r+1, r]);
        sMeasurements = sMeasurements & objClass.Measurements[r, r+1]);
        sMeasurements = sMeasurements & objClass.Measurements[r+1, r+1]);
        r++;
    }

    Ming Man
    chanmm
    • Marked as answer by Bin-ze Zhao Friday, March 27, 2009 3:33 AM
    Tuesday, March 24, 2009 5:46 AM