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.
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.
}
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:
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.