0

first post from newbie after having spend three days googling and reading MSDN:

From C# I'm trying to call a method in a VB dll which starts like this:

Public Function Correl(action As String, ByRef returns As Object, observation_frequency As Double) As Double(,)
Dim left As String = Strings.UCase(Strings.Trim(action))
Dim num As Double = observation_frequency
Dim objectValue As Object = RuntimeHelpers.GetObjectValue(returns)
Dim num2 As Short
Dim num3 As Short
Dim num4 As Short
Dim array As Double(,)
Dim arg_5D_0 As Short
Dim num5 As Short
' The following expression was wrapped in a checked-statement
num2 = CShort(Information.UBound(CType(objectValue, Array), 1))
num3 = CShort(Information.UBound(CType(objectValue, Array), 2))
num4 = num2 + 1
objectValue = RuntimeHelpers.GetObjectValue(returns)
array = New Double(CInt((num2 + 1)), CInt((num3 + 1)))
arg_5D_0 = 1
num5 = num2
For num6 As Short = arg_5D_0 To num5
    Dim arg_65_0 As Short = 1
    Dim num7 As Short = num3
    For num8 As Short = arg_65_0 To num7
        array(CInt(num6), CInt(num8)) = Conversions.ToDouble(NewLateBinding.LateIndexGet(objectValue, New Object()() = { num6, num8 }, Nothing))
    Next
Next '...........

When calling the method from C# like this

double[] dcloses1 = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
double[,] dcloses2 = new double[,] { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } };
object ocloses = (object)dcloses1; // or dcloses2
double[,] nFullMatrix = corobj.Correl("AVC", ref ocloses, (double)frequency); 

I get an error from second argument (ByRef returns As Object), originating from line 12 (num2 = CShort(...) like this:

  1. Passing single dimensional array (dcloses1), error is: "Argument 'Rank' is not valid for the array." True as it expects a N:N matrix, but at least the array is passed.

  2. Passing two dimensional array (dcloses2), error is: "Value cannot be NULL. Parameter name: Argument 'Array' is Nothing." So seems arraya inb object is not recognized.

Seems that the method expects an array wrapped into an object as it uses GetObjectValue(), but all intents so far to hand over the needed matrix have failed.

Have called this before from Excel VBA passing variant w/o any issue, but also C# VariantWrapper has not helped.

Any idea on how to structure the object for the second argument? Any similar experience between C# and VB interop?

Update Over the last day have tried all other structures, lists, array forms I'm aware off, but somehow without any success. Still same error "Value cannot be NULL. Parameter name: Argument 'Array' is Nothing.". Any other idea I could try?

AlexH
  • 21
  • 4
  • It seems there is no need for `ByRef` (VB) and `ref` (C#), just `returns as Object` should work. BTW, the `Variant` equivalent in C# is `dynamic`. – Wagner DosAnjos Jan 22 '14 at 03:50
  • If I leave `ref` out then compiler says "2nd argument has to be passed using `ref`". Using `dynamic` did not help. Missed to mention that I do not have the VB dll code, so changing method there is no option. – AlexH Jan 22 '14 at 04:12
  • Don't cast the array to `Object` - simply pass the array itself into the method like this: `double[,] nFullMatrix = corobj.Correl("AVC", ref dcloses, (double)frequency);`. The call to `GetObjectValue` will either return a boxed copy of the object if it's a value class, otherwise the object itself, as per this [MSDN linke](http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.runtimehelpers.getobjectvalue%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1). – Tim Jan 22 '14 at 05:37
  • Problem is that the method calls for `ref object`, so when I try to pass the `ref double[,]` w/o casting to object I get error `unvalid arguments` and cannot cast `double[,]` to `object`. I agree that the `GetObjectValue` would take it. Any way to circumvent / change the signature from the method? – AlexH Jan 22 '14 at 06:01

0 Answers0