locked
How to assign a string[] array contained in an object[] array to a string[] array var? RRS feed

  • Question

  • I have an object[] array (object[] o1) which is an argument parameter in a .Net to com Interop library I created.  o1 will receive a variant array containing 2 string[] arrays from a com (VBA -- Excel) call.  I assign the values of o1 to a string var and the output that gets returned to my VBA procedure is this:

    -- System.String[]System.String[]


    I tried declaring string[] s2; in the .Net lib procedure  and then assigning it like this:  s2 = o1[i]

    but I get this error message when I try to build the .Net lib

    Error 1 Cannot implicitly convert type 'object' to 'string[]'. An explicit conversion exists (are you missing a cast?) 

    Is there a way I can make this work?


    Here is my actual scenario (with the source code): 

    What I want to do is to pass an array of string[] arrays (basically trying to pass a jagged array to .Net from com VBA)  from com (VBA) to a .Net com Interop library I created -- and what I really need to do is to iterate through the string[] arrays contained in the object[] array.  If I use the following notation in the .Net lib

    object[][] o1  (or string[][] s1)

    and make a call in my VBA (Excel) procedure -- VBA doesn't support that notation.  However if I use the single array notation -- object[] o1 -- in the .Net lib and pass a variant array which contains 2 string arrays -- the VBA call works.  But I need to iterate through these string arrays in the .Net lib procedure.

    The .Net lib procedure looks something like this (right now):

    public string testProc(object[] o1)
    {
      string s1 = "";
      for (int i = 0; i < o1.Length; i++)
      {
         s1 += o1[i];
      }
     
      return s1;
    }

    and in VBA I do this which renders the following output:

    Sub TestTheLib()
      Dim myEx As New ExcelToAdoNet.myExcelToAdoNet
      Dim v1(1) As String, v2(2) As String
      Dim v3(1) As Variant

      v1(0) = "a1"
      v1(1) = "a2"
      v2(0) = "b1"
      v2(1) = "b2"
      v2(2) = "b3"
      v3(0) = v1
      v3(1) = v2

      Debug.Print myEx.testProc(v3)
    End Sub

    Output:

    -- System.String[]System.String[]

    Is there a way I can retrieve the content of these string arrays in my .Net library prodedure?


    Rich P


    • Edited by Rich P123 Thursday, April 3, 2014 6:00 PM ......
    Thursday, April 3, 2014 5:57 PM

Answers

  • How depressing :(.  It sounds like I can't pass an array of arrays to .Net from com.  My alternative is to pass individual arrays -- which I was hoping to avoid.  Oh well.

    That is absolutely not what I said!

    I said you can, but you have to explicilty cast it. In specific, this code is valid (maybe not the array initliasers, but those don't really matter):

    string[] array1 = { "Hello", "world" };
    string[] array2 = { "goodby", "cruel", "world" };
    string[] array3 = { "Lorem", "Ipsum" };
    
    //This will implictly cast array 1-3 into object.
    object[] ojArray = { array1, array2, array3 };
    /*Identical result too
    object[] ojArray = { (object)array1, (object)array2, (object)array3 };*/
    
    //This will implictly cast object[] to object
    object someObject = ojArray;
    
    //Now let's try to get them back. Explictly unpack the object too object[]
    object[] newOjArray = (object[])someObject;
    
    //Explicitly unpack one of the string array string[] array4 = (string[])newOjArray[0];
    Of course the big question is why you are taking objects or object[], if you expect this stuff to be string's? There should be a proper mapping type for COM Interop.

    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.



    • Edited by Christopher84 Thursday, April 3, 2014 7:04 PM
    • Marked as answer by Rich P123 Thursday, April 3, 2014 8:07 PM
    Thursday, April 3, 2014 7:03 PM

All replies

  • I have an object[] array (object[] o1) which is an argument parameter in a .Net to com Interop library I created.  o1 will receive a variant array containing 2 string[] arrays from a com (VBA -- Excel) call.  I assign the values of o1 to a string var and the output that gets returned to my VBA procedure is this:

    -- System.String[]System.String[]


    I tried declaring string[] s2; in the .Net lib procedure  and then assigning it like this:  s2 = o1[i]

    but I get this error message when I try to build the .Net lib

    Error 1 Cannot implicitly convert type 'object' to 'string[]'. An explicit conversion exists (are you missing a cast?) 

    Is there a way I can make this work?

    The compiler will autoamtically (implcitly) cast if there will be no loss of value with that conversion. It will cast a 8-bit-int to a 16, 32, 64 or 128 one. Almost anything can be transformed into a string (using Object.ToString()). It can cast or box anything towards object (as this is the most inclusive type).

    It will not go the other way around. It wants you to explicilty tell it to cast (so if you end up with something other then string[] in that object, it is your fault and your responsobility to fix). Doing implicit casting with chance of value loss or exceptions will only introduce hard to track bugs, so no Compiler worth it's money will do that.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Thursday, April 3, 2014 6:24 PM
  • It will not go the other way around. It wants you to explicilty tell it to cast (so if you end up with something other then string[] in that object, it is your fault and your responsobility to fix). Doing implicit casting with chance of value loss or exceptions will only introduce hard to track bugs, so no Compiler worth it's money will do that.

    How depressing :(.  It sounds like I can't pass an array of arrays to .Net from com.  My alternative is to pass individual arrays -- which I was hoping to avoid.  Oh well.


    Rich P

    Thursday, April 3, 2014 6:32 PM
  • How depressing :(.  It sounds like I can't pass an array of arrays to .Net from com.  My alternative is to pass individual arrays -- which I was hoping to avoid.  Oh well.

    That is absolutely not what I said!

    I said you can, but you have to explicilty cast it. In specific, this code is valid (maybe not the array initliasers, but those don't really matter):

    string[] array1 = { "Hello", "world" };
    string[] array2 = { "goodby", "cruel", "world" };
    string[] array3 = { "Lorem", "Ipsum" };
    
    //This will implictly cast array 1-3 into object.
    object[] ojArray = { array1, array2, array3 };
    /*Identical result too
    object[] ojArray = { (object)array1, (object)array2, (object)array3 };*/
    
    //This will implictly cast object[] to object
    object someObject = ojArray;
    
    //Now let's try to get them back. Explictly unpack the object too object[]
    object[] newOjArray = (object[])someObject;
    
    //Explicitly unpack one of the string array string[] array4 = (string[])newOjArray[0];
    Of course the big question is why you are taking objects or object[], if you expect this stuff to be string's? There should be a proper mapping type for COM Interop.

    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.



    • Edited by Christopher84 Thursday, April 3, 2014 7:04 PM
    • Marked as answer by Rich P123 Thursday, April 3, 2014 8:07 PM
    Thursday, April 3, 2014 7:03 PM
  • Very cool!  Now I get it.  (I'm so happy :)  I can stay with passing an array of arrays -- yay.  For my purposes this is way better than passing individual arrays.  As for com (VBA) I have not been able to come up with anything that is native to VBA to deal with this -- I have only been able to use a variant array (VBA -- object array in C#) to hold the string arrays.

    Many Thanks


    Rich P

    Thursday, April 3, 2014 8:07 PM