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.
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.
Return an array, containing the values.
you could also use the "ref" parameter
that would change the values of the passed parameter.
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
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
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.
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
// [b]Cast One[/b] - 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];
// [b]Cast Two[/b] - 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 - [B]Preferred Where possible[/B]
// 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.
As to not leave people thinking that there is only one way. You can use the + to return more then one value.
return item1 + item2;
That will give you two values out without having to use object classes etc....
As to not leave people thinking that there is only one way. You can use the + to return more then one value.
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....
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:
public int[] Returnmethod()
{
int a = 1;
int b = 2;
return new int[] {a , b };
}
Mitja
To me an array can be considered as ONE value.
A method that returns two values could be:
static int toreturn(int inint, out bool bbool)
{
if (inint == 42) bbool = true;
else bbool = false;
return inint*2;
}
This would return the double value of inint and [the return of B]bbool [/B]via out, would only be true if the initial value of inint is equal to 42.
Ok brain fart... I was wrong... disregard this. I was trying to delete it but can't....
Yeah, I was thinking. I was like... Um, I'm a newbie, but even I know that that's either concatenating or adding them :D
Nice way to resurrect a 6 year old thread :)
Nice way to resurrect a 6 year old thread :)
Lol I just realized. Plus, that wasn't my fault, rollo did it :D
you can also try this:
If you have a function that sets 2 Global variables(or arrays if nesecarry) then the function doesn't need to return anything. when you need the values again you simply call them from the Globals class.
Here is an example:
public static class Globals
{
public static string name = "";
public static int age = 0;
}
public void setValues(string value1, int value2)
{
Globals.name = value1;
Globals.age = value2;
}
class program
{
Console.WriteLine("Name: " + Globals.name);
Console.WriteLine("Age: " + Globals.age);
}
The simplest way to return multi values from a method is to create List.. if you have same class parameter then you have to create that class list and if you have different class values then simply make object class list
1.class check
2.{
3. public list<string> returnValue()
4. {
5.
6. list<string> value=new list<string>();
7. value.add("hello");
8. value.add("world");
9. value.add("2012");
10. return value;
11. }
12.
13. public void getvalue()
14. {
15. list<string> getvalue=returnValue();
16. for(int i=0;i<getvalue.Count;i++)
17. {
18. Console.WriteLine(getvalue[i].ToString());
19. } }
20 }
Nice way to resurrect a 7 year old thread :)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.