简体   繁体   English

C#使用IF / ELSE语句中的方法返回的值

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

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;
        }
    }

}

You can simplify this down to the following code, which will only call those methods once and make the code much more readable: 您可以将其简化为以下代码,该代码只会调用这些方法一次,并使代码更具可读性:

public class ShopFloorDBRepository
{
    public static Domain.ShopFloor.ShopFloorObject GetShopFloorOrder(string workOrd)
    {
        return Domain.ShopFloor.Shopfloor.GetOrder(workOrd) ??
               Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(workOrd);
    }
}

To explain why this works - the ?? 解释为什么这样做有效-?? operator (the null-coalescing operator!) basically says "if the returned value on the left hand side of the ?? is null, then return the value of the expression on the right hand side". 运算符(null-coalescing运算符!)基本上说:“如果??的左侧返回的值为null,则返回右侧的表达式的值”。

This way you only need to call your functions once. 这样,您只需要调用一次函数。

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);
    if(yowo != null)
    {
        return yowo;
    }

    /* default return */
    return null;

}

PS 聚苯乙烯

You're kinda doing the "Factory Pattern" 您正在执行“工厂模式”

See 看到

http://www.dofactory.com/net/factory-method-design-pattern http://www.dofactory.com/net/factory-method-design-pattern

Looks to me like you could be using a temporary variable to hold the result, which you can test and return. 在我看来,您可能正在使用一个临时变量来保存结果,您可以对其进行测试并返回。

public class ShopFloorDBRepository
{
  public static Domain.ShopFloor.ShopFloorObject GetShopFloorOrder(string WorkOrd)
  {
    var result = Domain.ShopFloor.GetOrder(WorkOrd);

    if (result != null) return result;
    ...

This is a common paradigm, especially when the method being called is expensive and/or has side effects you don't wish to incur twice. 这是一个常见的范例,尤其是当所调用的方法昂贵且/或具有您不希望发生两次的副作用时。

Here, the "var" declaration sets the type of "result" to the type returned by the method being called; 在这里,“ var”声明将“结果”的类型设置为被调用方法返回的类型。 you could also use the name of the actual type. 您也可以使用实际类型的名称。

If you wish to make two different kinds of tests like this, you'll need two different variables unless they have the same type (which in this case it appears they do). 如果您希望像这样进行两种不同的测试,则需要两个不同的变量,除非它们具有相同的类型(在这种情况下,看起来确实如此)。

An alternate mechanism that does require the full type, that you'll also see: 另一种需要完整类型的机制,您还将看到:

public static ShopFloorObject GetShopFloorOrder(string WorkOrd)
{
    ShopFloorObject result;

    if ( (result = Domain.ShopFloor.GetOrder(WorkOrd)) != null )
        return result;
    if ( (result = Domain.DG9_DeliveryPerformance.DG9_DeliveryPerformance.GetObject(WorkOrd)) != null)
        return result;
    return null;

Here you're explicitly declaring the type of the return value, then making the two calls you've indicated, testing the results against null, and returning the first non-null value. 在这里,您明确地声明了返回值的类型,然后进行了所指示的两次调用,针对null测试结果,并返回第一个非null值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM