-
November 21st, 2010, 07:59 AM
#1
Returning multiple strings
Hi,
I realise that it is not technically possible to return multiple strings, and that they must be aggregated in some way. I was wondering what the best way to go about this might be. The number of strings needed to be returned each time is variable from 0 to infinity.
I have though about returning an array of strings but I do not want to have a hard limit defined by the number of array elements declared. I will not know until runtime how many strings I need to return, so I don't want to have some arbitrary number of fixed array elements.
The best idea I have some up with is to concatenate all of the strings together, separated by a known divider character, return them, and then split them up afterwards. This seems like a bit of a nasty hack to me.
Does anyone have any other suggestions? Is there more of a 'conventional' way to do something like this?
Thanks.
Thanks.
-
November 21st, 2010, 09:36 AM
#2
Re: Returning multiple strings
concatenate all the strings is extremely inefficient, and even if it was, it's generally not a good idea to threat 1000 strings as 1 string.
I'd return a List<string> object. it's basically a "dynamic array of strings" - you can add as much elements to it as you want during run-time.
Code:
List<string> strings = new List<string>();
strings.Add("Some random string");
strings.Add("Another random string");
Console.WriteLine(strings[0]); //"Some random string"
for(int i=0;i<100000;i++)
strings.Add(i.ToString());
strings.RemoveAt(1); //removes the 2nd element from the list
Console.WriteLine(string.Count.ToString()); //100001
-
November 22nd, 2010, 04:23 PM
#3
Re: Returning multiple strings
Thanks for your response. However the ArrayList solves all of my issues - I am from a C/C++ background so this was a completely new concept to me.
I am able to declare a new ArrayList inside a function that returns an ArrayList. I simply add as many strings as required and then return the entire ArrayList. The number of elements matches the number of strings exactly (unlike an arbitrarily sized array of strings which is only partially used).
-
November 22nd, 2010, 05:06 PM
#4
Re: Returning multiple strings
Originally Posted by infra
Thanks for your response. However the ArrayList solves all of my issues - I am from a C/C++ background so this was a completely new concept to me.
I am able to declare a new ArrayList inside a function that returns an ArrayList. I simply add as many strings as required and then return the entire ArrayList. The number of elements matches the number of strings exactly (unlike an arbitrarily sized array of strings which is only partially used).
Firstly, it shouldn't be. C++ has STL containers which work like the C# generic containers.
Secondly, you should take Talikag's advice and use List<string> not ArrayList.
ArrayList is depreciated. It's slower, and it's memory footprint is larger. It has less features. Most importantly, it is not strongly typed.
-
November 22nd, 2010, 11:17 PM
#5
Re: Returning multiple strings
Yep, prefer List< T > over the ArrayList( ).
-
November 23rd, 2010, 07:46 PM
#6
Re: Returning multiple strings
Originally Posted by Chris_F
Firstly, it shouldn't be. C++ has STL containers which work like the C# generic containers.
Alas, there are so many self-proclaimed "C++ developers" who know little of C++ and are basically writing C code.
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
|