Results 1 to 5 of 5

Thread: arrayList to string[] ?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    arrayList to string[] ?

    I dont know why I always have trouble workign with array lists. I want to convert an array list to an array of strings. I tried this but I get an error:
    string[] result=(string[])arrConvos.ToArray (typeof(string[]))

    the error is: "At least one element in the source array could not be cast down to the destination array type."


    this works well, but shouldnt there be another way for this? why doesnt the above code work?
    Code:
        result= new string[arrConvos.Count-1];
        for (int i=0; i<result.Length; i++)
            result[i] = arrConvos[i].ToString();
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    Re: arrayList to string[] ?

    Originally posted by MrPolite
    I dont know why I always have trouble workign with array lists. I want to convert an array list to an array of strings. I tried this but I get an error:
    string[] result=(string[])arrConvos.ToArray (typeof(string[]))

    the error is: "At least one element in the source array could not be cast down to the destination array type."


    this works well, but shouldnt there be another way for this? why doesnt the above code work?
    Code:
        result= new string[arrConvos.Count-1];
        for (int i=0; i<result.Length; i++)
            result[i] = arrConvos[i].ToString();
    I think it is because you are putting an item in the arraylist that isn't a string. But when you do that second piece of code, you are taking that item and calling it's ToString() method which will convert it to a string and make it work.

    Inspect your arraylist in the debug window. Foreach through it and do a debug.writeline on it to see if any type held in the arraylist is not of type string.

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    try this
    Code:
    string[] result=(string)arrConvos.ToArray ();

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    this works:
    PHP Code:
    string[] result = ( string[] )arrConvos.ToArraytypeofSystem.String ) ); 

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    thank you
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width