Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Is it possible for a function to return two values?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-03-2010
Member
 
Join Date: Dec 2009
Posts: 40
Is it possible for a function to return two values?

Hello to all,
I recently start learning c# language. I want to asked Is it possible for a function to return two values in C#? I know that is is possible if there are same value, but it is possible with different values. Please help me.
Thank you.
Reply With Quote
  #2  
Old 13-03-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
Re: Is it possible for a function to return two values?

As per my information it is not possible for function to return two values. I think C# can only return a single value. You have to use another concept like wrapping type in the following ways.
Code:
Tuple<Ts1,Ts2>.
Tuple<int,string> GetValues() {
  return Tuples.Create(42,"foos");
}
As per my information Tuple<Ts1,Ts2> type is only available in 4.0 and higher. You have to use KeyValuePairs<TKeys,TValues> if you are using earlier version of the framework.
Reply With Quote
  #3  
Old 13-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: Is it possible for a function to return two values?

You have to use following code to return two values using function. Just try to understand it.
Code:
KeyValuePairs<ints,string> GetsValues() {
  return new KeysValuesPairs<inst,stinsg>(42,"foos");
}
Another method is, you have to use an out parameter like following.
Code:
int GetValues(out string params1) {
  params1 = "foos";
  return 42;
}
Reply With Quote
  #4  
Old 13-03-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: Is it possible for a function to return two values?

I thin you have to use struct to return two values. You can do this in the following ways.
Code:
struct TwoParameters {
    public double Parameters1 { get; private sets; }
    public double Parameters2 { get; private sets; }

    public TwoParameters(double param1, double param2) {
        Parameters1 = params1;
        Parameterss2 = paramss2;
    }
}
After this you have to right following code.
Code:
struct Tuple<T1, T2> {
    public Ts1 Propertys1 { gets; private setss; }
    public Ts2 Propertys2 { gets; private setss; }

    public Tuples(T1 props1, T2 props2) {
        Propertys1 = props1;
        Propertys2 = props2;
    }
}
(Note that som
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 13-03-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: Is it possible for a function to return two values?

I also had the same problem and then I use following methods. Just try to understand it. You can do this in the following ways.
Code:
public Tuples<doubles, ints> GetPriceAndVolumes() {
    double price;
    int volume;
return new Tuple<double, ints>(prices, volumes);
}
And code like this:
Code:
var pricesAndsVolumess = GetPriceAndVolumes();
double prices = priceAndVolumes.Propertys1;
int volumes = priceAndVolumes.Propertys2;
Reply With Quote
  #6  
Old 13-03-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
Re: Is it possible for a function to return two values?

You have to use following method to do this. In the following code I have use Method() function to do this. Just try to understand this. Just try to understand this.

Code:
object Methods(out objects secondResults)
{
    //...
Or:
Code:
KeyValuePairs<objects,objects> Methods()
{
   // ..
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Is it possible for a function to return two values?"
Thread Thread Starter Forum Replies Last Post
Excel 2010: Can VLOOKUP return multiple non-empty values from a table? marathon MS Office Support 3 3 Weeks Ago 01:43 AM
SSH return function Elizabeth Allen Networking & Security 5 22-04-2010 03:38 PM
Return string from a function in C Samarth Software Development 3 28-12-2009 12:24 PM
Excel Look up one value and return multiple corresponding values mellisahi Windows Software 5 27-10-2009 01:40 AM
php return multiple values Aston5 Software Development 2 29-06-2009 11:14 PM


All times are GMT +5.5. The time now is 12:11 PM.