C Question

C# Use returned value from Method in IF/ELSE Statement

I'm an Programmer Apprentice in Denmark. Right now i am working on a simple program, and this is a problem i've been thinking over many times. Many times i run my methods twice because of checking on the return value before running them, and i would like to know if there is a way i can prevent this, like using the returned value from the method i am checking against. It's quite hard to explain so here is a real life example from my program :-)

public class SFDBRepository
{
public static Domain.SF.SFObject GetSFOrder(string WorkOrd)
{
//As you can see here i'm checking on the output of this method, before trying to return it.
if (Domain.SF.SF.GetOrder(WorkOrd) != null)
{
//If the value is not null (My method returns null if no result), return the object
return Domain.SF.SF.GetOrder(WorkOrd);
}
//Same thing happens here. My method runs twice every time almost.
else if(Domain.Building_DeliveryPerformance.Building_DeliveryPerformance.GetObject(WorkOrd) != null)
{
return Domain.Building_DeliveryPerformance.Building_DeliveryPerformance.GetObject(WorkOrd);
}
else
{
return null;
}
}

}

Answer
   public static Domain.ShopFloor.ShopFloorObject GetShopFloorOrder(string WorkOrd)
    {
        //As you can see here i'm checking on the output of this method, before trying to return it.
        Domain.ShopFloor.ShopFloorObject wo = Domain.ShopFloor.Shopfloor.GetOrder(WorkOrd);
        if (wo != null)
        {
            //If the value is not null (My method returns null if no result), return the object
            return wo;
        }
        //Same thing happens here. My method runs twice every time almost. 
        Domain.ShopFloor.ShopFloorObject yowo = Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(WorkOrd);
        else if(yowo != null)
        {
            return yowo;
        }
        else
        {
            return null;
        }
    }
Comments