944,148 Members | Top Members by Rank

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

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 41136
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 6th, 2005
0

How to return more than one value from a method?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cumadhu is offline Offline
19 posts
since Sep 2005
Sep 6th, 2005
1

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

Return an array, containing the values.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Sep 6th, 2005
0

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

you could also use the "ref" parameter
that would change the values of the passed parameter.
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Sep 7th, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Madhusudhan is offline Offline
3 posts
since Aug 2005
Sep 9th, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
halil is offline Offline
1 posts
since Sep 2005
Sep 10th, 2005
0

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

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Kate Albany is offline Offline
71 posts
since Jun 2005
Oct 4th, 2005
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SethWebster is offline Offline
7 posts
since Apr 2005
Mar 17th, 2011
0
Re: How to return more than one value from a method?
As to not leave people thinking that there is only one way. You can use the + to return more then one value.
C# Syntax (Toggle Plain Text)
  1. return item1 + item2;
That will give you two values out without having to use object classes etc....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rollo1002 is offline Offline
2 posts
since Mar 2011
Mar 17th, 2011
0
Re: How to return more than one value from a method?
Click to Expand / Collapse  Quote originally posted by rollo1002 ...
As to not leave people thinking that there is only one way. You can use the + to return more then one value.
C# Syntax (Toggle Plain Text)
  1. return item1 + item2;
That will give you two values out without having to use object classes etc....
Ok brain fart... I was wrong... disregard this. I was trying to delete it but can't....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rollo1002 is offline Offline
2 posts
since Mar 2011
Mar 17th, 2011
0
Re: How to return more than one value from a method?
As SethWebster explained, you return an array.
If there are more value with different type (int, decimal, string, ...) you return an object array.
If there are all the values of the same type, you can return the same type:
C# Syntax (Toggle Plain Text)
  1. public int[] Returnmethod()
  2. {
  3. int a = 1;
  4. int b = 2;
  5. return new int[] {a , b };
  6. }

Mitja
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Run Only One Copy using Mutex
Next Thread in C# Forum Timeline: How to opet text file and print in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC