1.11M Members

Returning Multiple Objects

 
0
 

Hi All,

I have a question and can't seem to find a answer anywhere. A normal function can void or return a object.

public void CoolFunction()
{
[INDENT]//Some Code[/INDENT]
}

I know you can do this.

public int ReturnOneObject()
{
[INDENT]return intNumber[/INDENT]
}

What I can't find or even know is possible is a function that returns two objects.

public int, bool MultiReturnFunction()
{
[INDENT] return intNumber, blBool[/INDENT]
}

Any ideas?

 
1
 

you can't return more than object with different data type
but may use yield it may help you.

 
0
 

Thx

 
1
 

It is true that a method can only return one value, however that value can be an array of values.

private object[] getSomeData()
{
   object[] mydata = {256, "Here's you daddy"};
  return mydata;
}
 
// to use the method
object[] somedata = getSomeData();
MessageBox.Show(string.Format("The number is {0}, the string is {1}", (int)somedata[0], (string)somedata[1]) );

You can also pass into a method some reference variables for the method to set.

--Jerry

 
0
 

Thx

I have concluded that if you have to return multiple values from a function that is called your probably didn't think something logically correct to begin with.

Rather than trying to find a way you should treat this a a red flag that something is not right.

Thx!

 
1
 

Thx

I have concluded that if you have to return multiple values from a function that is called your probably didn't think something logically correct to begin with.

Rather than trying to find a way you should treat this a a red flag that something is not right.

Thx!

Its possible to return multiple values from a function by using "out" in argument list

You can use the following code.

public void CoolFunction(in int a, out int b, out c){
//Some Code
}

//Call the above function as,
CoolFunction(2, out int b, out c);after this u can get values from b,c.

You can refer the following code as a example.
-------------------------------------------------------------------------------------------------------
using System;
//class that contains a method

public class Passingvalues{

public void Calculatesquarecube(int a,out int d,out int c){
d=a*a;
c=a*a*a;
}
}

//another class
public class Application
{
public static void Main(){
int a=2;
int d,c;
Passingvalues opassinvalues=new Passingvalues();

opassinvalues.Calculatesquarecube( a,out d,out c);
System.Console.Write("the square and cubes are {0} and {1}",d,c);
}
}
-------------------------------------------------------------------------------------------------------

 
0
 

That is so sly but makes perfect sense. I knew about ref and out but was thinking too small as in 1 variable. Bad on my part.

Thx

 
0
 

it's not returning more than object it's passing object by reference!!

 
0
 

it's not returning more than object it's passing object by reference!!

There is always more then one way to do anything. For certain situations it will fit the need.

 
0
 

s/he didn't ask me to do something by anyway, s/he asked to return more than object I answered.
anyway thank you, sure I knew something new by your answer :)

 
0
 

Blacklocist,
Just out of curiousity,

What would one expect the syntax to be for a function that returned more than one value ?

string x = foo(); // which might return 2 strings
What would someone expect x to be after the call ? the first string ? the second ?
Then, how would you use x ? string c = x.part[0]; ???

What language have you used that would support such a thing, and can you provide an example..

just curious..

 
0
 

Blacklocist,
Just out of curiousity,

What would one expect the syntax to be for a function that returned more than one value ?

string x = foo(); // which might return 2 strings
What would someone expect x to be after the call ? the first string ? the second ?
Then, how would you use x ? string c = x.part[0]; ???

What language have you used that would support such a thing, and can you provide an example..

just curious..

1. If C# supported multiple return from functions in my mind I would imagine it to go something like this.

string strString1 = this.ReturnTimeDate(1);
string strString2 = this.ReturnTimeDate(2);

public string,string ReturnTimeDate()
{

[INDENT]//Must be same number of returns as declared in the function declaration
return DateTime.Now.TimeOfDay.ToString();
return DateTime.Now.Date.ToString();[/INDENT]
}

2. I haven't used a language that returns multiple objects from a function or method. I was just thinking and for some unknown reason that question came to the front of my scattered mind. :)

 
0
 

First of all, there is no possible way for a function to use the return keyword to return more than one value. remember, the return keyword, not only returns a value out to another variable (if mentioned) but also returns the whole control back to the function that called the current function. all the subsequent statements WILL be ignored.

using System;
namespace Test
{
    class ReturnDemo
    {
        static string Return_Demo()
        {
            return "World!";
            Console.Write(" World...");
        }

        static void Main()
        {
            Console.Write("Hello " + Return_Demo());
            Console.ReadLine();
        }
    }
}

If you compile it you'll automatically get a warning message saying that the code cannot be reached.
There is no programming language anywhere that can use return statement twice in a function and be able to use them both.

 
0
 

ChaseVoid,

Yea, we know it can't be done (atleast not that way). I was just trying to get into the poster's head to see what his thoughts were. After programming for 24 years, I'd never see someone ask that question.

Now that I see where his head is at, he could do something like this (however totally inefficient)

private string[] foo()
{
  string[] result = {"one","two","three"};
  return result;
}
 
mytextBox.Text = foo()[1];
myotherbox.Text = foo()[2];

or to follow his thoughts a little closer:

private string[] MyStrings = {"One","Two"};
private string foo(int index)
{
    return MyStrings[index];
}

Anyway, I think BlackLocist got the idea.


//Jerry

 
0
 

Yes I completely agree not efficient by any means. I wasn't thinking quite straight when I asked the question but now after the feedback I have received there are always more than one way to do something. I was just curious if there is something C# doesn't do besides pointers.

Wait nvm C# can use pointers.

I guess weird questions/thoughts have a common origin of lack of sleep + hunger. lol

Thx All!

Question Answered as of 6 Years Ago by Ramy Mahrous, JerryShaw, pointers and 1 other
 
0
 

haahaa.. lack of sleep + hunger, is like the best.. kekeke.. Anyways, C# can however use ref keyword for to point to the same memory location.also Arrays are pointers to that only point to the first memory location of the array. lol, but the pointers like C, i don't think there is, but thee might be many alternatives.

 
0
 

Here I found a good solution to the problem.
http://www.ugmfree.it/TipsCSharp.aspx?tip=TipCSharpReturnParams

The magic is obtained using Tuple.
So the output parameters become type safe too.

As much as I love getting this info on using templates, Please DO NOT revieve dead threads. Aslo if you only need like two returns from a fuction, there is no need to use templates. It's just more annoying to manage.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article