| | |
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 | |||
![]() |
•
•
Join Date: Apr 2005
Posts: 7
Reputation:
Solved Threads: 0
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:
Hope that this helps.
// 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.
![]() |
Similar Threads
- Super-power compare method? <?> (Java)
- Covariant return types (Java)
- Parameters order (Java)
- Need help debugging program (Java)
- How to invoke array varibles to another main method (Java)
- How to use collection in java (Java)
- Got Fractions? Method help w/ logic issue (Java)
- How to maintain session for Web Services Object? (VB.NET)
- Error getting forms to send information (PHP)
Other Threads in the C# Forum
- Previous Thread: Is it even possible to read MPEG-4's
- Next Thread: REMOTING, .NET has got a problem wit me *smile*
| Thread Tools | Search this Thread |
.net access algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client clock combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees developer development draganddrop drawing dynamiccreation encryption enum excel file form format forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql operator password path photoshop picturebox pixelinversion post priviallages. programming property radians regex remoting richtextbox running... serialization server sleep soap socket sql sqlserver stack statistics stream string table temperature text textbox thread time timer update usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf write xml






