Casting an arraylist of integers to a string array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Earl

    Casting an arraylist of integers to a string array

    I'm trying to do a cast and I'm not sure where I'm going wrong here. I load
    up the ColorExtIntID column (int) into a list array (no problem there). For
    binding to a 3rd party component, it has to then be cast to a string array.

    private void PopulateColors( )
    {

    DataView dvColors = new DataView(dtColo rs);

    ArrayList ColorIDList = new ArrayList(dvCol ors.Count);

    Int32 i;

    for (i = 0; i <= dvColors.Count - 1; i++)

    {

    ColorIDList.Add (dvColors[i]["ColorExtIn tID"]);

    }

    Int32 x = fpSpread1.Activ eSheet.ActiveRo w.Index;

    // this is the invalid cast where I get an exception

    string[] sColorIDList = (string[])ColorIDList.To Array(typeof(In t32));

    .....

    }


  • allfyre

    #2
    Re: Casting an arraylist of integers to a string array

    try changing the last line to:

    string[] sColorIDList = (string[])ColorIDList.To Array(typeof(st ring));

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Casting an arraylist of integers to a string array

      Earl <brikshoe@newsg roups.nospamwro te:
      I'm trying to do a cast and I'm not sure where I'm going wrong here. I load
      up the ColorExtIntID column (int) into a list array (no problem there). For
      binding to a 3rd party component, it has to then be cast to a string array.
      It can't. A cast doesn't change the contents, it just provides the
      compiler with more information about what is present.

      You'll need to create a new string array of the same size, then convert
      each int to a string.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      Working...