I am fetching an unknown number of strings from a database and I want to store these in a string array. I am trying something like this: string[] myStrings; length = 0; while(dataReader.Read()) string[length++] = new string(dataReader.getString(0)); This doesn't
work and I can't figure out how to do this. I tried using the abstract Array object and Collections, but it seems none of them works very well unless I specify firsthand how many elements i need. But I don't know how many. Isn't there a string array object
in C# that has a simple add method for adding new strings dynamically? I need to pass the finished string[] array to a method that accepts only string[] type. Any tips much appreciated! Stian
Use the ArrayList. The array object is immutable by design to be lightweight and therefore once it is initialized the dimensions cannot be changed. The ArrayList is designed to be a dynamic array object.
Thanks for the response. I tried doing that, but failed when I wanted to send it as a strings[] parameter to a function. Is there a way to easily turn the ArrayList object into a strings[] array, or do I have to create a strings[] array in addition and copy
the contents over to it? Valein
Well - you wouldn't be able to pass an ArrayList object into a method that expects an Array object. You can do a couple of things: 1) Either change the signature of the method to accept an ArrayList or 2) use the ArrayList.ToArray method to pass the contents
into a newly created Array object.
Valein
Member
270 Points
54 Posts
A very simple string question
Sep 05, 2003 01:25 PM|LINK
satarter
Member
270 Points
54 Posts
Re: A very simple string question
Sep 05, 2003 02:42 PM|LINK
Valein
Member
270 Points
54 Posts
Re: A very simple string question
Sep 05, 2003 03:06 PM|LINK
satarter
Member
270 Points
54 Posts
Re: A very simple string question
Sep 05, 2003 04:50 PM|LINK