Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Is this correct for returning multiple values of different data types from a function?

$
0
0

I need to return a string result and a bool value from a function, so I created an object[] return type.  It seems to work, but I am checking to see if this is the correct approach or if there is a better practice approach for doing something like this. 

private void btnTestf_Click(object sender, EventArgs e)
{
   object[] o1 = testMultiObjs();
   Console.WriteLine(o1[0].ToString());
   Console.WriteLine(o1[1].ToString());
}

private object[] testMultiObjs()
{
   bool b1 = true;
   string s1 = "test";
   object[] o1 = new object[2];
   o1[0] = b1;
   o1[1] = s1;
   return o1;
}

Actually, in my original implementation of my function -- it was just supposed to return a bool value, but if something err'd out (in a try/catch block) -- I want now to return a bool (of false) and the err message.  Of course, if no err's then return the bool value and some happy message.

Lastly, just a question -- in my function if I have a for loop that is checking for something and finds it and sets boolVal = true

bool boolVal;
for (int i = 0; i < o1.Length; i++)
{
   ...
   if (boolVal == true)
      return boolVal;
}

...

boolVal = false;

return boolVal;


does the return statement in the for loop act like a break to break out of the function once boolVal == true?

I am guessing yes.

or would this function go all the way to the bottom and set boolVal = false if I don't break out the for loop after boolVal == true?


Rich P



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>