Home > DeveloperSection > Forums > Only one return() for the entire method
Goti Bandu

Total Post:119

Points:835
Posted on    November-13-2013 7:21 AM

 C# C# 
Ratings:


 1 Reply(s)
 606  View(s)
Rate this:

I want a single return statement instead of having a return statement in each if statement. Can anybody tell me how to do it?

public object GetHeaderInfo(string agentId, string headerName)

 {

     if (headerName == "flyer")

     {

        var headerInfo = Service.GetFlierHeaderInfo(agentId);

        // headerinfo is of type Flier object

        return headerInfo;

     }

     if (headerName == "general")

     {

         var headerInfo = Service.GetHeaderInfo(agentId);

         // headerinfo is of type report object

         return headerInfo;

     }

     return 0;

 }



ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    November-13-2013 7:46 AM

Hi Goti,

Declare it at the top of your method :

public object GetHeaderInfo(string agentId, string headerName)

{

    object headerInfo = 0;

    if (headerName == "flyer")

    {

        headerInfo = Service.GetFlierHeaderInfo(agentId);

    }

    if (headerName == "general")

    {

        headerInfo = Service.GetHeaderInfo(agentId);

    }

    return headerInfo;

}

The default value can be added during the initialization, it will be overwritten by any future assignation.

Don't want to miss updates? Please click the below button!

Follow MindStick