Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple results from a method [duplicate]

Tags:

c#

return

I trying to improve my skills using Try Catch blocks and better error handling.

I have a class that performs a common task, in this case retrieving a Facebook AccessToken. If successful, I want to return the AccessToken string, if not I want to return an error message. These are both strings, so no problem. But when checking the return value on the calling side of the code, how can you do this effectively?

It's like I need to return 2 values. In the case of a successful attempt, return = true, "ACESSCODEACXDJGKEIDJ", or if it fails, return = false, "Ooops, there was an error" + ex.ToString();

Then checking the return value is easy (in theory). I could think of returning simply a true/false for return and then setting a Session variable for the strings.

What is a way to return multiple results from a method?

like image 446
crichavin Avatar asked Jan 19 '12 17:01

crichavin


People also ask

Can you return multiple values in a method?

You can return only one value in Java. If needed you can return multiple values using array or an object.

Can you return multiple values from a function using return statement?

If we want the function to return multiple values of same data types, we could return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.

What are the different method for returning multiple values from function?

Below are the methods to return multiple values from a function in C: By using pointers. By using structures. By using Arrays.

Can you return multiple variables in one method?

You cannot explicitly return two variables from a single function, but there are various ways you could concatenate the two variables in order to return them.


2 Answers

Create a Result class and return that instead...

public class Result
{
   public bool Success {get;set;}
   public string AccessToken {get;set;}
   public string ErrorMessage {get;set;}
}


public Result GetFacebookToken()
{
   Result result = new Result();

   try{
      result.AccessToken = "FACEBOOK TOKEN";
      result.Success = true;
   }
   catch(Exception ex){
      result.ErrorMessage = ex.Message;
      result.Success = false;
   }

   return result;
}

Then you can call this code like...

Result result = GetFacebookToken();

if(result.Success)
{
   //do something with result.AccessToken
}
else
{
   //do something with result.ErrorMessage 
}
like image 193
musefan Avatar answered Sep 22 '22 16:09

musefan


2 possibilities spring to mind

  1. Use the TryXXX pattern (used in some BCL methods such as DateTime.TryParse).
  2. Design a class that contains the status of the operation and the result and then have your method return this class.

Let's first see the TryXXX pattern. It's basically a method that returns a boolean value and the result as out parameter.

public bool TryXXX(string someInput, out string someResult, out string errorMessage)
{
    ...
}

which will be consumed like this:

string someResult;
string errorMessage;
if (!TryXXX("some parameter", out someResult, out errorMessage))
{
    // an error occurred => use errorMessage to get more details
}
else
{
    // everything went fine => use the results here
}

In the second approach you would simply design a class that will contain all the necessary information:

public class MyResult
{
    public bool Success { get; set; }
    public string ErrorMessage { get; set; }

    public string SomeResult { get; set; }
}

and then have your method return this class:

public MyResult MyMethod(string someParameter)
{
    ...
}

which will be consumed like this:

MyResult result = MyMethod("someParameter");
if (!result.Success)
{
    // an error occurred => use result.ErrorMessage to get more details
}
else
{
    // everything went fine => use the result.SomeResult here
}

Of course the results can be any other complex object instead of (as shown in this example) a string.

like image 34
Darin Dimitrov Avatar answered Sep 21 '22 16:09

Darin Dimitrov