none
Unable to cast object of type 'System.String[*]' to type 'System.String[]'.

    Question

  • "In C# I am getting this error message. "Unable to cast object of type 'System.String

  • ' to type 'System.String[]'." However, in the debugger the object is indeed String[] and not String
  • . What is String
  • ? Can I create a variable of this type?"

     

    string theword = "ferries";

    object thelanguage = WdLanguageID.wdEnglishUS;

    GlobalClass theGlobalClass = new GlobalClass();

    SynonymInfo SimObj = theGlobalClass.get_SynonymInfo(theword, ref thelanguage);

    if (SimObj.Found)

    {

    if (SimObj.RelatedWordList != null)

    {

    string TheWord = ((string[])(SimObj.RelatedWordList))[1];

    {

    System.Console.WriteLine("similar word ");

    }

    }

    }

Saturday, May 05, 2007 6:45 AM

Answers

  • Thanks for the replies above. A co-worker just provided the code that works.

     

    string theword = "ferries";

    object thelanguage = WdLanguageID.wdEnglishUS;

    GlobalClass theGlobalClass = new GlobalClass();

    SynonymInfo SimObj = theGlobalClass.get_SynonymInfo(theword, ref thelanguage);

    if (SimObj.Found)

    {

    if (SimObj.RelatedWordList != null)

    {

    IEnumerable wordlist = (IEnumerable)SimObj.RelatedWordList;

    foreach (object o in wordlist)

    {

    string word = (string)o;

    }

    }

    }

    Monday, May 07, 2007 7:45 PM

All replies

  • What is RelatedWordList, method or property, and of what type it is.
    Saturday, May 05, 2007 11:17 AM
  • If your RelatedWordList property is a string array you do not need to cast it before accessing it's items.

    Basically, just saying
    Code Snippet

    string TheWord = SimObj.RelatedWordList[1];


    should give you the second item in the array.

    Note that the first item in an array is at index 0 not 1.
    Saturday, May 05, 2007 2:05 PM
  • Thanks for the reply. RelatedWordList is from the SynonymInfo interface in the Microsoft.Office.Interop.Word namespace. RelatedWordList is of the type "object". But it is an object that boxes an array of strings. In the debugger it shows its type as object {string[]} and has a value of {Dimensions: [1..2]}.

     

    The only variable I have been able to assign it to is type object. But I am trying to get to the strings in the array.

     

    Trying to get an array member without casting gets a compile error.

    Trying to cast to string[] compiles but gets the message above at run.

    I get a compile error when I try to cast it as string

  •  

    I am trying to get the output of this

    public virtual Microsoft.Office.Interop.Word.SynonymInfo get_SynonymInfo(string Word, ref object LanguageID)

    Member of Microsoft.Office.Interop.Word.GlobalClass

     

    thanks!

Saturday, May 05, 2007 10:30 PM
  • I should have mentioned that it is of type object. It just boxes a string array. It does not compile without the cast because I cannot assign an object to string[]. Any ideas?

     

    thanks-

    Saturday, May 05, 2007 10:31 PM
  • Looks like RelatedWordList is actually a multidimensional array!
    I would try

    Code Snippet

    string TheWord = ((string[,])(SimObj.RelatedWordList))[0,0];


    Saturday, May 05, 2007 11:55 PM
  • Thanks. But now I get

    Unable to cast object of type Sysytem.String[asterisk symbol] to type System.String[ , ].

    Do you know what System.String[asterisk symbol] means? (Note the symbol is used but I cannot use it in this message.) Does it mean an unbounded or finite array when the other is finite or unbounded?

     

    thanks!

    Sunday, May 06, 2007 12:24 AM
  • The asterisk indicates that the object RelatedWordList is a pointer array.
    I don't have word installed on machine so i'm unable to reproduce the error.
    Sunday, May 06, 2007 2:16 AM
  • Thanks! This is likely a stupid question...but how can I define and cast to a pointer array in C#?
    Sunday, May 06, 2007 2:42 AM
  • In C# it seems that there cannot be a pointer to a string. Can you clarify the response?
    Sunday, May 06, 2007 5:03 AM
  • Should I instead be implementing the interface SynonymInfo and making RelatedWordList just a string[] instead of a object {string[]} ?
    Sunday, May 06, 2007 6:27 AM
  • Thanks for the replies above. A co-worker just provided the code that works.

     

    string theword = "ferries";

    object thelanguage = WdLanguageID.wdEnglishUS;

    GlobalClass theGlobalClass = new GlobalClass();

    SynonymInfo SimObj = theGlobalClass.get_SynonymInfo(theword, ref thelanguage);

    if (SimObj.Found)

    {

    if (SimObj.RelatedWordList != null)

    {

    IEnumerable wordlist = (IEnumerable)SimObj.RelatedWordList;

    foreach (object o in wordlist)

    {

    string word = (string)o;

    }

    }

    }

    Monday, May 07, 2007 7:45 PM
  • I'd just like to correct something:

    If you ever see something like this:


    System.String[ * ]

     

    That does NOT mean an array of pointers!

    What it actually means is that the array is not 0-based. Most likely the array will be 1-based and will originate from Visual Basic. But you don't know for sure - you only know that it is not 0-based.

    The only way to create arrays that are not 0-based in C# is to use Array.Create() with an appropriate set of arguments. For example:


    Array a = Array.CreateInstance(typeof(string), new int[]{0}, new int[]{1});
    Console.WriteLine(a.GetType()); // Writes "System.String[ * ]"

     


    Monday, May 07, 2007 8:05 PM
  • Thanks for the  correction Matthew.
    Tuesday, May 08, 2007 12:40 PM
  • 2013-11-30 17:02 (-5 GMT)

    Hola.

    Quien te estaba ayudando parece que no tiene idea de los que le estabas preguntando. Es decir, tú lo explicaste bien el problema, y para quien ha trabajado con office, es evidente el problema generado por la conversión de tipos.

    En mi caso, lo siguiente me ha funcionado:

            if (wordlist.MeaningCount > 0)
            {
                //object Lista = (object)wordlist.SynonymList[wordlist.MeaningCount];
                object Lista = (object)wordlist.MeaningList;

                foreach (var meaning in Lista as Array)
                {
                   string Sinonimo = meaning.ToString());

                   //Aqui puedes agregar el código necesario para almacenar o mostrar al usuario los sinónimos encontrados.
                }
                   
            }

    Espero que te sea de utilidad.
    • Edited by a12l Saturday, November 30, 2013 10:31 PM
    Saturday, November 30, 2013 10:07 PM