How to return more than one value from a method?

Please support our C# advertiser: Intel Parallel Studio Home
View Poll Results: Can a method return more than one value?
Yes 4 80.00%
No 0 0%
Can't say 0 0%
May be 1 20.00%
Voters: 5. You may not vote on this poll

Reply

Join Date: Sep 2005
Posts: 19
Reputation: cumadhu is an unknown quantity at this point 
Solved Threads: 0
cumadhu cumadhu is offline Offline
Newbie Poster

How to return more than one value from a method?

 
0
  #1
Sep 6th, 2005
Hi All,
I have specific requirement in C#.
I have to return two values from a single variable/method.
How to do it?

If you can help me, you are most welcome.

Thanks and with regards,

Madhusudhan.H.K.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: How to return more than one value from a method?

 
0
  #2
Sep 6th, 2005
Return an array, containing the values.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: How to return more than one value from a method?

 
0
  #3
Sep 6th, 2005
you could also use the "ref" parameter
that would change the values of the passed parameter.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: Madhusudhan is an unknown quantity at this point 
Solved Threads: 0
Madhusudhan Madhusudhan is offline Offline
Newbie Poster

Re: How to return more than one value from a method?

 
0
  #4
Sep 7th, 2005
Can i Use 'out' keyword with respect to string(as return values) insted of 'ref'.
I dont know how to work with 'out' attribute.
Can you send code sample for the same.

Thanks and with regards,
Madhusudhan
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1
Reputation: halil is an unknown quantity at this point 
Solved Threads: 0
halil halil is offline Offline
Newbie Poster

Re: How to return more than one value from a method?

 
0
  #5
Sep 9th, 2005
hi
you can return an address of each array avec C#,when you cashe over one value in this array , you can return over one value
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 71
Reputation: Kate Albany is an unknown quantity at this point 
Solved Threads: 1
Kate Albany Kate Albany is offline Offline
Junior Poster in Training

Re: How to return more than one value from a method?

 
0
  #6
Sep 10th, 2005
Is C# not an OO language, can you not just return an Object cantaining the two attributes. By returning a Class (Object) you have much more freedom.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 7
Reputation: SethWebster is an unknown quantity at this point 
Solved Threads: 0
SethWebster SethWebster is offline Offline
Newbie Poster

Re: How to return more than one value from a method?

 
0
  #7
Oct 4th, 2005
Late, I know, but -- Kate is correct. In the case that you want to return two variables of different types, you can simply return an array of object, or a Class created to contain those different types. Lets look at a couple of different ways to accomplish this:

// Old school not very OOP way to accomplish your task
public object[] MultiReturnMethodOne(string Param1, int Param2)
{
     // DO WORK
     return new object[]{result1,result2,...};
}

public YourClassName MultiReturnMethodTwo(string Param1, int Param2)
{
     // DO WORK
     YourClassName result = new YourClassName(Result1, Result2);
     return result;
}

// OOP Class used in Method 2
public class YourClassName
{
     // declare our private fields
     private string _result1=string.Empty;
     private int _result2=0;

     // constructor
     public YourClassName(string Result1, int Result2)
     {
          this._result1 = Result1;
          this._result2 = Result2;
     }

     // Gets our first Result (string)
     public string Result1
     {
          get
          {
               return _result1; 
          }
     }
     // Gets our Second Result (string)
     public int Result2
     {
          return _result2;
     }     

}

public void testMultiReturn()
{
     // TEST METHOD ONE
     object[] result = MultiReturnMethodOne("test1",10);
     // Now we have our results, but here we dont know exactly
     // what they are so we have to cast them to 
     // their respective types
     // There are more than two ways to do this, but here are the
     // most common
     // Cast One - Common but may fail
     // this method will fail if the result[0] is not a string
     // or if result[1] is not an int
     // so use this in the case that you don't have any question
     // as to the results
     string result1 = (string)result[0]; 
     int result2 = (int)result[1]; 
     // Cast Two - Safer but less informative as to why it 
     // didn't work if it fails
     // safer -- this second method will not fail, but result1 will be null
     // if the result[0] is not a string 
     // or result2 will equal its default value (0) if result[1] is not an
     // integer
   
     result1 = result[0] as string;  
     result2 = result[1] as string;
     // END TEST METHOD ONE

     // TEST METHOD TWO - Preferred Where possible 
     // As Kate said, why not use a class or a struct?
     // See above for definitions
     YourClassName ycn = MultiReturnMethodTwo("test1",10);
     // Now we have a nicer result
     Console.WriteLine("String Result {0}, Int Result {1}",
          ycn.Result1, 
          ycn.Result2);
     // Much more concise, and OOP.
}

Hope that this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC