Search code examples
c#arraysadd-insolidworks

Issue with out system.Array Conversion for solidworks plugin C#


I just got thrown into a C# project for solidWorks that I'm not too comfortable with. I need to convert this out System.Array to a string[]. Then that string is called and converted from out System.Array to out EdmLib.EdmBatchError2[].

TLDR: out System.Array' to a string[].

Code:

private void GetSerialNumberGenerators() 
{
    IEdmSerNoGen7 utility = this.m_vault.CreateUtility(EdmUtility.EdmUtil_SerNoGen) as IEdmSerNoGen7;
    Array ppoRetNames = Array.CreateInstance(typeof(string[]), 0);
    utility.GetSerialNumberNames(out ppoRetNames);
    this.comboBoxSerialNumber.DataSource = (object) ppoRetNames;
}

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'out System.Array' to 'out string[]'


Solution

  • It's simple as

    string[] ppoRetNames;
    GetSerialNumberNames(out ppoRetNames);
    

    This is the way to declare a string[]. Don't initialize it yourself because GetSerialNumberNames will do it (out-parameter). No need to use Array.CreateInstance.

    Apart from that you are creating a jagged array because you pass typeof(string[]) not typeof(string). You need a one dimensional array so this would be correct:

    Array someArray = Array.CreateInstance(typeof(string), 0);
    string[] ppoRetNames = (string[])someArray;  // so a cast is what was missing