|
-
Oct 15th, 2004, 03:59 PM
#1
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!!
-
Oct 15th, 2004, 10:11 PM
#2
PowerPoster
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.
-
Oct 16th, 2004, 01:55 PM
#3
Frenzied Member
try this
Code:
string[] result=(string)arrConvos.ToArray ();
-
Oct 18th, 2004, 12:00 AM
#4
Hyperactive Member
this works:
PHP Code:
string[] result = ( string[] )arrConvos.ToArray( typeof( System.String ) );
-
Oct 18th, 2004, 12:46 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|