InvalidCastException Object [*] for object []

I have a COM object written in Visual Fox Pro 9.0. It performs the following procedure:

    PROCEDURE GetArray(m.vcArrayName as String) as array
RETURN @&vcArrayName

      

The COM object is referenced in a VS2010 project using C #. Procedure signature in C #:

object GetArray(string vcArrayName);

      

When debugging I see that the return value is {object [1..1]} and the type is {object []}. Expanding the variable, I see that it is a base 1 array with one element, which is an empty string ("");

However, I cannot pass this value to the [] object. I always get an InvalidCastException with a Norwegian message saying I cannot use Object [*] for Object [].

The weird thing is that if the COM object returns a 2D array, I have no problem casting it into a [,] object. I find it very strange that two dimensions are easier to deal with than one!

The question I want to answer is the following:

What is this [*] object? Can anyone tell me if this is a bad translation in the Norwegian exception post, or if Object [*] is some obscure C # syntax that I haven't heard of?

+2


a source to share


2 answers


You end up with a multidimensional array with size 1 instead of a vector. C # does not allow such an array to be declared. You can reference the return value with the Array class, which allows you to convert code like this:



public static object[] ConvertFoxArray(Array arr) {
  if (arr.Rank != 1) throw new ArgumentException();
  object[] retval = new object[arr.GetLength(0)];
  for (int ix = arr.GetLowerBound(0); ix <= arr.GetUpperBound(0); ++ix)
    retval[ix - arr.GetLowerBound(0)] = arr.GetValue(ix);
  return retval;
}

      

+5


a source


object[]

is a vector in CLI terms; these should be 0-based and 1-dimensional. object[*]

is an array that is one-dimensional and may even (although not in your case) even be 0-based. But it is not a vector.

Instead of casting, you will need to copy the data to object[]

. I made the same mistake here .

You can use CopyTo

:



array.CopyTo(vector, 0);

      

(where array

- object[*]

, a vector

- object[]

)

+4


a source







All Articles