Search code examples
iphoneobjective-cxcodemethodsout-parameters

how can return two string from a method separately without appending?


I have method that will receive a string input(with message & mode name) and the method will separate the string input in to two strings(1.message 2.mode name) according to separator. but i need to return this separated two string at a time.please provide me a good way to do this.can i use "out" and return in one method? if yes, please tell me any links to do the out parameter. (or) any good way if you have.


Solution

  • You can use out parameters:

    string myMethod(string input, out secondOutput)
    {
       secondOutput="bla";
       return "xyz";
    }
    

    you can also use 2 out parameters:

    void myMethod(string input, out firstOutput, out secondOutput)
    {
       firstOutput="bla";
       secondOutput = "xyz";
    }
    

    Or like others suggested and I think is preferrable most of the time, using an array:

    string[] myMethod(string input)
    {
       return new string[] {firstOutput, secondOutput);
    }
    

    If you can tell us what the method is for we might help you choosing the best alternative.