Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to retrieve more than one values from a function?

(string1,string2) = sourceValues(sourceId);

public string sourceValues(string sourceId)
{
#do something
return string1, string2;
}
Posted

You can do this:
C#
Tuple<string,string> strTuple = sourceValues(sourceId);
// now strTuple.Item1 is the first string, and strTuple.Item2 is the second string

public Tuple<string,string> sourceValues(string sourceId)
{
// do something
return new Tuple<string,string>(string1,string2);
}


Hope this helps.
 
Share this answer
 
Comments
Member 9353131 1-Nov-12 8:50am    
Thanks it helped me a lot.
member60 1-Nov-12 23:24pm    
my 5!
Hi,

We should return the Collection from the functions.

Say for example, I am searching for list of Buses that are travelling from given source to destination.

public List<string> GetBusInformation(string strSource, string strDestination)
{
List<string> objBusInformation=new List<string>();

// Here Write the Logic for finding the buses when the Bus is found it should be added to List<string>

// return the List<string> object.

return objBusInformation
}

Regards,
RK
 
Share this answer
 
Here is some samples,


C#
using System;
using System.Collections.Generic;

class Program
{
    static void GetTwoNumbers(out int number1, out int number2)
    {
    number1 = (int)Math.Pow(2, 2);
    number2 = (int)Math.Pow(3, 2);
    }

    static KeyValuePair<int, int> GetTwoNumbers()
    {
    return new KeyValuePair<int, int>((int)Math.Pow(2, 2), (int)Math.Pow(3, 2));
    }

    static void Main()
    {
    // Use out parameters for multiple return values.
    int value1;
    int value2;
    GetTwoNumbers(out value1, out value2);
    Console.WriteLine(value1);
    Console.WriteLine(value2);

    // Use struct for multiple return values.
    var pair = GetTwoNumbers();
    Console.WriteLine(pair.Key);
    Console.WriteLine(pair.Value);
    }
}



regards and thanks
sarva
 
Share this answer
 
Use generic list as shown below to retrieve more than one values from a function

C#
public List<string> sourceValues(string sourceId)
{
#do something
return List<string's)>;
}</string>
 
Share this answer
 
One of the option is to create list. Example:

XML
public list<string> sourceValues(string sourceId)
{
#do something
list<string> value=new list<string>();
value.add("string1");
value.add("string2");
return value;
}

you can retreive this by:

list<string> getvalue=returnValue(sourceID);
-- and loop through to get the values
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900