Click here to Skip to main content
15,887,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
From some time I try to solve this problem without any success.
Any help would be greatly appreciated.

I have external library DLL with needed methods to do some calculation on inputs and gives me the output (I don't have acces to code)
------------------------------------------------------
The working excel VB script to call this library is
VB
Dim Inputs() As Double
Dim Outputs() As Variant
ReDim Inputs(1 To 1, 1 To NbrInputs&)
For i& = 1 To NbrInputs&
Inputs(1, i&) = CDbl(InRange(i&).Value)
Next i&

VB
ABC.CalculateMatrix(Inputs#, Outputs)

------------------------------------------

I have great problem interfacing the library with C#
the method should be in C#
ABC.CalculateMatrix(ref System.Array, ref System.Array)
The input and output arrays do not seem to work.I've tried nearly everything

What I have tried:

C#
Double[] Inputs = new Double[4];
Object[] Outputs = new Object[1]; 
ABC.CalculateMatrix(Inputs, Outputs);

And the error i get is
System.Runtime.InteropServices.COMException'
Object variable or With block variable not set
Posted
Updated 25-Apr-18 21:31pm

Quote:
ReDim Inputs(1 To 1, 1 To NbrInputs&)

That creates a two-dimensional array with a non-zero lower bound. To duplicate that in C#, you would need:
C#
Array Inputs = Array.CreateInstance(
    typeof(double), 
    new[] { 1, NbrInputs }, // lengths
    new[] { 1, 1 });        // lower bounds

for (int i = 1; i <= NbrInputs; i++)
{
    double value = Convert.ToDouble(InRange[i].Value);
    
    // NB: Can't use indexer to set the value for a non-zero lower bound:
    Inputs.SetValue(value, 1, i);
}

It's not clear from the code you've posted what needs to be passed to the Outputs parameter. You would need to check the documentation of the third-party code to see if that gives you any clues.
 
Share this answer
 
Comments
Maciej Los 25-Apr-18 14:02pm    
Hawk eye!
I overlooked that it's a two-dimensional array!
Member 13797060 26-Apr-18 2:27am    
The DLL was written probably in VB quite some time ago 2012
Member 13797060 26-Apr-18 3:32am    
Only using this type of variant gives me the different error in c#

dynamic Output=new dynamic[3];

Element „object” does not contain function CalculateMatrix
ABC.CalculateMatrix expects references:

System.Array Inputs = Array.CreateInstance(typeof(Double), 4);
System.Array Outputs = Array.CreateInstance(typeof(Object), 1);
ABC.CalculateMatrix(ref Inputs, ref Outputs);
 
Share this answer
 
v2
Comments
Member 13797060 25-Apr-18 4:03am    
//After change to ABC.CalculateMatrix(ref Inputs, ref Outputs);
I get following errors:

cannot convert from 'ref double[]' to 'ref System.Array'
cannot convert from 'ref object[]' to 'ref System.Array'
[no name] 25-Apr-18 4:09am    
You can use Array.CreateInstance to create arrays of the correct type in that case. I've updated my original reply with an example.
Member 13797060 25-Apr-18 4:14am    
After changing to
System.Array Inputs = Array.CreateInstance(typeof(Double), 4);
System.Array Outputs = Array.CreateInstance(typeof(Object), 1);
ABC.CalculateMatrix(ref Inputs, ref Outputs);

I get following ERROR
Object variable or With block variable not set
[no name] 25-Apr-18 4:47am    
That seems like a VB error thrown by ABC.CalculateMatrix, instead of a problem with the way of calling from C#. I hope someone else can help with the VB part, since I'm not experienced with that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900