Function
-
Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)
If C# supports a VARIANT you could do it that way, so long as both types were able to be stuffed into a VARIANT. Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOz
I live in Bob's HungOut now
-
Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)
You could also use the
out
keyword.public bool OurFunction (out object value2)
{
bool value; //return value;//do whatever...
if (value)
value2 = "This object returned true!!!";
else
value2 = new string [] {"This object", "returned", "true", "!!!"};return value;
}Or you could simply just return an
object
and then check the type wheter it's true of false...public object OurFunction2 ()
{
bool value;
object value2;// do whatever...
if (value)
value2 = "This object returned true!!!";
else
value2 = new string [] {"This object", "returned", "true", "!!!"};return value2;
}public void MainFunction ()
{
object obj = this.OurFunction2 ();
if (obj.GetType () == typeof (string)) //true
Console.WriteLine ("TRUE!!!")
else // false...
Console.WriteLine ("FALSE!!!");
}:) Andreas Philipson
-
Is it possible to write a function that return 2 different data types? e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string. Thanks alot! ;)
It could return object, which could either be a string or an array of string. In general, however, I think that's a bad idea. Why not return an array with a single item for the first case?