Link to home
Start Free TrialLog in
Avatar of mhouldridge
mhouldridge

asked on

return (More than one variable?)

Experts,

Is it possible to return more than one variable, ie. Exception error as string and a bool value false, so that I can output the error and also update the database if value is false?

Something like:

bool trueorfalse
Exception exc
return exc, trueorfalse;

I've tried that, the syntax is wrong, please help!
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

No

but you can use out params

private bool MyFunc(out string test)
{
     test = "A Value";
      return true;
}

string myString;
bool ret = MyFunc(out myString);
Or return a class/struct with multiple properties....
Avatar of gambit_642
gambit_642

Pass the bool by reference, and assign your bool in the function.
I think crazyman's second recommendation is the best. Using that method you can set the return type once, and not worry about adding extra variables to the method as you need them.
How about something like this.

        private void button1_Click(object sender, EventArgs e)
        {
            Object[] myObject;
            myObject = MyFunction();

            bool myBool = (bool)myObject[0];
            Exception myExc = (Exception)myObject[1];

        }

        private Object[] MyFunction()
        {
            Object[] retObj = new Object[2];
            bool trueorfalse = true;
            Exception exc = new Exception();

            // Do some things

            retObj[0] = trueorfalse;
            retObj[1] = exc;
            return retObj;
        }
Agreed, a struct is useful for this


public struct MyStruct
{
       private int variable1;
       private string variable2;
       public MyStruct(int v1,string v2) //Remember paramterless constructor not allowed
       {
           variable1 = v1;
           variable2 = v2;
       }
       public int Variable1
      {
             get{return variable1;}
      }
       public string Variable2
      {
             get{return variable2;}
      }
}
 
then
 
public  MyStruct Test(int param1)
{
        return new MyStruct(param1,"This is a test");
}
 
then 
MyStruct s = Test(100);
s.Variable1 etc...

Open in new window

Avatar of mhouldridge

ASKER

How do you return a class struct with multiple variables:

I have Form1 class (the main class) with a seperate Try class.

Within the try I have;

public static string TryConnection {
       
       try {
       do something
       }
       catch
       {
       return  error// CURRENT ONE
       RunClass // OR SHOULD I RUN A CLASS TO RETURN MULTIPLES AS BELOW
       }

}


private bool RunClass(out string error, out bool truefalse)
{
string error = "error";
bool turefale = true;
return true;
}
FernandoSoto.

I deliberatley didnt mention that method, i dont think its good practise at all as the caller has no idea of the expected results of the function, and if your using value types your boxing/unboxing which will have a performance penalty, however it is perfectly valid....
I've seen that a struct can cause more overhead, and using a out variables is the way to do this...

As a beginner, using out variables reads easier, and Im really confused about the struct way...
I think Fernandosoto's solution is the best, returning an array;

How about something like this.

        private void button1_Click(object sender, EventArgs e)
        {
            Object[] myObject;
            myObject = MyFunction();

            bool myBool = (bool)myObject[0];
            Exception myExc = (Exception)myObject[1];

        }

        private Object[] MyFunction()
        {
            Object[] retObj = new Object[2];
            bool trueorfalse = true;
            Exception exc = new Exception();

            // Do some things

            retObj[0] = trueorfalse;
            retObj[1] = exc;
            return retObj;
        }


What do you think, it's deffinately the easiest to understand, and I think the most practical.
I have the following:

In main Form1 Class:

                   // Run PortCHECK class to check server and return multiple values

                    Object[] myObject;
                    myObject = PortCheck.TryServer(IPaddress, port);

                    bool ConnectionAlive = (bool)myObject[0];
                    string ConnectionMessage = (string)myObject[1];


                   TextBox.ForeColor = Color.Green;
                   TextBox.Text += "Trying..." + IPaddress + " " + stringport + Environment.NewLine;
                   TextBox.ForeColor = Color.White;
                   TextBox.Text += myObject[0];
                   TextBox.Text += myObject[1];
                   TextBox.Text += Environment.NewLine;
                   this.Refresh();


The above code runs the PortCheck.TryServer method which is  - public static object TryServer

And in there I have:

            // RETURN VALUES
            Object[] retObj = new Object[2];
            bool trueorfalse = true;

            retObj[0] = trueorfalse;
            retObj[1] = ServerResponse;
            return retObj;


I get the error:

Cannot implicitly convert type 'object' to 'object[]'. An explicit conversion exists (are you missing a cast?)

What does this mean?
On what line are you getting the error on?
PortCheck.TryServer(IPaddress, port);
The value types being returned are correct.... Im confused!
Ah, it's because on my class it was:

public static Object MyClass

When it should have been

public static Object[] MyClass

So does object with [] mean it's an array of objects, whereas Object is simply one object?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial