Hi! I just started learning C# and stucked with this problem I encountered while accessing a VB6 OCX function from C#.
VB6 OCX Code is:
Public Function AreaByCoords(ByRef X() As Double, ByRef Y() As Double) As Double
Dim L As Long
Dim H As Long
Dim I As Long
Dim A As Double
L = LBound(X)
H = UBound(X)
'
If (H - L) < 3 Then Exit Function
'
A = 0
For I = L To H
A = A + Y(I) * IIf(I = H, X(L), X(I + 1)) - X(I) * IIf(I = H, Y(L), Y(I + 1))
Next I
AreaByCoords = A / 2
End Function
Public Function IAdd(A As Double, B As Double) As Double
IAdd = A + B
End Function
C# Code:
private void btnTest_Click(object sender, EventArgs e){
double A; double B; double V; double[] C;C =
new double[4];C[0] = 1.542;
C[1] = 2.285;
C[2] = -2.234;
C[3] = -2.521;
double[] D;D =
new double[4];D[0] = 1.642;
D[1] = -2.585;
D[2] = -2.234;
D[3] = 2.826;
A = 2.345;
B = 4.321;
V = axP1029.IAdd(
ref A, ref B); //This runs ok! MessageBox.Show(Convert.ToString(V)); //This runs ok!V = axP1029.AreaByCoords (
ref C, ref D); //Error! MessageBox.Show(Convert.ToString(V));}
Running the above code, the following errors are displayed:
Error 1 The best overloaded method match for 'AxPo1030.AxP1029.AreaByCoords(ref System.Array, ref System.Array)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'ref double[]' to 'ref System.Array'
Error 3 Argument '2': cannot convert from 'ref double[]' to 'ref System.Array'
Any idea how to set this right? All suggestions are much appreciated. Thank you in anticipation.
AlanPosted Oct 31, 2008, 12:23 PM
See if it will work if you do:
Array arrC = C;
Array arrD = D;
V = axP1029.AreaByCoords (ref arrC, ref arrD);
AlanPosted Oct 31, 2008, 5:19 PM
Ah, so the IIf() function was evaluating all its arguments even when I equalled H which caused the X and Y arrays to exceed their bounds by one.
I'd always thought IIf() was a pseudo-function like C#'s ternary operator (?:) but it appears that (in VB6 at least) it works like a normal function.
Anyway, makes perfect sense now, Rod :)
Rod PerdidoPosted Oct 31, 2008, 4:49 PM
You are right Alan. The error was coming from the VB6 function. I modified it and everything worked like a charm. Your first suggestion worked well and so with the 2nd one. Thanks again. I appreciate it much. :)
The modified VB6 function:
Public Function AreaByCoords(ByRef X() As Double, ByRef Y() As Double) As Double
Dim L As Long
Dim H As Long
Dim I As Integer
Dim A As Double
L = LBound(X)
H = UBound(X)
'
If (H - L) < 3 Then Exit Function
'
A = 0
For I = L To H
If I = H Then
A = A + Y(I) * X(L) - X(I) * Y(L)
Else
A = A + Y(I) * X(I + 1) - X(I) * Y(I + 1)
End If
Next I
AreaByCoords = A / 2
End Function
AlanPosted Oct 31, 2008, 3:48 PM
That error is, presumably, bubbling up from the VB6 function though as both arrays have lengths of 4, it's difficult to see why :-/
My only other suggestion is to try making the two arrays entirely late-bound:
Array C = Array.CreateInstance(typeof(double), 4);
C.SetValue(1.542, 0);
C.SetValue(2.285, 1);
C.SetValue(-2.234, 2);
C.SetValue(-2.521, 3);
Array D = Array.CreateInstance(typeof(double), 4);
D.SetValue(1.642, 0);
D.SetValue(-2.585, 1);
D.SetValue(-2.234, 2);
D.SetValue(2.826, 3);
V = axP1029.AreaByCoords (ref C, ref D);
Rod PerdidoPosted Oct 31, 2008, 1:32 PM
Hi Alan! Thank you for your suggestion. I tried it but I got a different error this time.
System.IndexOutOfRangeException {"Subscript out of range"}
How do I correct this? Thanks in advance.