-5

I am trying to make a selection for a set of values, each of the methods return a string array which I am trying to put into a string array in the main. Here is the code:

string[] array = { MethodA1(), MethodA2(), MethodA3() };

I am trying to make the string[] array, just one item. This works: (but it only allows the user to show/select one value)

string[] array = MethodA1();

Can anyone think of a way to allow multiple method returns into an array?

alex
  • 6,818
  • 9
  • 52
  • 103
Yrneh
  • 63
  • 3
  • 9
  • 2
    Try searching. http://stackoverflow.com/questions/1547252/how-do-i-concatenate-two-arrays-in-c, http://stackoverflow.com/questions/59217/merging-two-arrays-in-net, http://stackoverflow.com/questions/16012380/merge-2-arrays-using-linq, http://stackoverflow.com/questions/1177080/how-to-merge-several-arrays-in-a-list-using-linq – CodeCaster Apr 13 '15 at 12:57

3 Answers3

7

Something like this?

string[] array = MethodA1().Concat(MethodA2()).Concat(MethodA3()).ToArray();

Edit: It sounds from your comments that you are actually trying to achieve something entirely different: to select the results of one of these methods based on a parameter. Do you mean something like this?

private string[] SelectFiles(object userInput)
{
    string[] array;

    if (userInput == 1)
    {
        array = MethodA1();
    }
    else if (userInput == 2)
    {
        array = MethodA2();
    }
    else
    {
        array = MethodA3();
    }

    return array;
}
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Thanks for the reply, is it possible to select a specific method instead of displaying all of them? :) – Yrneh Apr 13 '15 at 13:07
  • Isn't that just `string array = MethodA1()` per your question? I think you might need to give some concrete examples of what you're trying to achieve, because I'm not sure I understand your question. – Charles Mager Apr 13 '15 at 13:09
  • sorry, the items in the string array are files, once placed in the string[] array, those are then send to another method which sorts them into ascending or descending order based on the users choice. I have tried an if statement like 'if(userInput == 1) { string[] array = {MethodA1();} ' but I cannot return the array out of an IF statement. I hope that helps? – Yrneh Apr 13 '15 at 13:16
  • I think you should edit your question to include this & some context (e..g more code!). You asked how to combine the results from multiple methods but it sounds like you actually want to choose which method to execute depending on some other parameter. None of this is expressed clearly in the question. – Charles Mager Apr 13 '15 at 13:19
5

You could use SelectMany to flatten everything to a single string[]:

string[] array = (new[] { MethodA1(), MethodA2(), MethodA3() })
    .SelectMany(a => a)
    .ToArray();
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

Maybe you should consider

List<string>

You could simply use

myList.AddRange(stringArrayMethod());
titol
  • 999
  • 13
  • 25