Cimbian
Cimbian

Reputation: 173

function return values c#

I am trying to get to grips with C# having not coded for many years and my previous experience being in ANSI C.

I have read a number of books and searched online but one aspect is evading me and I am hoping someone here can help.

In the past I would declare a function and if there was a possibility of something not happening within the function (i.e. file not found etc.) declare the return to be an integer. I would then return 0 if all was well and a value if not. The value would correspond to where the function failed to execute fully and I could branch accordingly from where I called it.

if(function1())
  {
   // all my error stuff, maybe a switch/case etc.
  }

All the examples I have found in C# seem to avoid this technique and I was hoping to get some understanding here.

Thanks in anticipation.

(I know I am a fossil). :)

Upvotes: 0

Views: 892

Answers (4)

Elshan
Elshan

Reputation: 7683

    if(function()==1)
     {

     }

    int function()
     {
      int returnVal =0;
      // do stuff
      // if true return  returnVal =1 else set returnVal =0;
      return  returnVal;
     }

Upvotes: 1

ChrisF
ChrisF

Reputation: 137108

There's nothing to stop you doing this (though there are better ways of handling the errors - exceptions for example).

If you do want to carry on with this approach, the biggest problem you are having is that in C# you can't treat an integer as a boolean so your if test won't compile. What you need is:

if (function1() != 0)
{
}

But to check the value you'd need:

int result = function1();
switch (result)
{
    case 1:
        // Handle this case
        break;
    case 2:
        // Handle this case
        break;
    default:
        // All OK
        break;
}

It would be better to return an enumerated type for each error case so that you don't have magic numbers, but exceptions are the way to go:

try
{
    function1();
}
catch (SpecificException1 e1)
{
    // Handle this case
}
catch (SpecificException2 e2)
{
    // Handle this case
}

What you shouldn't have is a general exception handler:

catch (Exception e)
{
}

This just hides other potential problems.

Upvotes: 2

Shadow Wizard
Shadow Wizard

Reputation: 66389

If you want to follow that pattern of checking return value instead of managing errors, you better use enumarations than plain numbers.

For example:

public enum ResultType
{
    Error = 0,
    Success,
    Waiting
}

public ResultType function()
{
    if (still_waiting)
        return ResultType.Waiting;

    if (error_has_occured)
        return ResultType.Error;

    return ResultType.Success;
}

public void Main()
{
    ResultType result = function();
    switch (result)
    {
        case ResultType.Success:
            MessageBox.Show("all is good");
            break;
        case ResultType.Waiting:
            MessageBox.Show("still waiting...");
            break;
        case ResultType.Error:
            MessageBox.Show("error has occurred");
            break;
    }
}

Behind the scenes, it's still using numbers but you put some meaning to each number.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174279

Exceptions are the approach you use in C# and similar languages.

It goes like this:

try
{
    function();
}
catch(FileNotFoundException e)
{
    // File not found
}
catch(UnauthorizedAccessException e)
{
    // User doesn't have right to access file
}
// etc...

To make this work, function shouldn't return a status code but instead throw an exception in case of an error.
Please note that the exceptions I illustrated in the code block above are thrown by the framework if you try to access a file and one of those errors is happening. So you don't actually have to do this yourself.


Furthermore, in C# there is no implicit conversion from integral values to bool, i.e. if(function()) is invalid, if function returns an int. You would need to write it like this:

if(function() != 0)
{
    // all your error stuff
}

Upvotes: 3

Related Questions