Returning Multiple Objects
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?
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2
you can't return more than object with different data type
but may use yield it may help you.
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2
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!
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2
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
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2
it's not returning more than object it's passing object by reference!!
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
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.
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2
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 :)
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
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. :)
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2
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!
blacklocist
Junior Poster in Training
87 posts since Apr 2006
Reputation Points: 10
Solved Threads: 2