簡體   English   中英

多個相等的if是否應從不同的方法(C#)返回不同的值?

[英]Multiple equal ifs should return different values from different methods (C#)?

我有一些使用if條件的方法,例如:

string method1()
{
    bool x= Convert.ToBoolean(...);
    bool y= Convert.ToBoolean(...);

     if (x && y)
     {
         return " ";
     }

     if (!x && y)
     {
         return " ";
      }

      if (!y && x)
      {
          return " ";
      }

      if (!x && !y)
      {
          return "X";
      }

      return " ";
  }

那就是我的第一個方法,現在我有一個類似的方法,它具有相同的檢查和相同的布爾值,但是返回其他字符串(不是空格或X)。 解決這個問題的方法是什么?

謝謝

根據您的代碼,如果x和y均為假,則僅返回“ X”。 在這種情況下,所有其他組合都應返回“”。 可以將其縮短,但為了便於閱讀,我將其保留下來。

method1("X", " ");
method1("OtherValue", " ");

string method1(string matchValue, string nonMatchValue)
{
    bool x= Convert.ToBoolean(...);
    bool y= Convert.ToBoolean(...);

      if (!x && !y)
      {
          return matchValue;
      }

      return nonMatchValue;
  }

為什么不將所需的返回值作為參數傳遞給方法?

string method1(string returnVal1, string returnVal2)
{
    bool x= Convert.ToBoolean(...);
    bool y= Convert.ToBoolean(...);

    if (x && y)
    {
        return returnVal1;
    }

    if (!x && y)
    {
        return returnVal1;
    }

    if (!y && x)
    {
        return returnVal1;
    }

    if (!x && !y)
    {
        return returnVal2;
    }

    return returnVal1;
}

優雅的方法是重構代碼,以便布爾檢查位於其他兩個方法均可訪問的自己的方法中,然后每個方法都可以基於布爾檢查的結果返回字符串。

private int BinaryTruthTest(bool x, bool y)
{
   if(x && y)
   {
      return 3;
   }
   if(!x && y)
   {
      return 1;
   }
   if(!y && x)
   {
      return 2;
   }
   if(!x && !y)
   {
      return 0;
   }
}

string method1()
{
   bool x = Convert.ToBoolean(...);
   bool y = Convert.ToBoolean(...);

   int testResult = BinaryTruthTest(x, y);

   if(testResult==0)
   {
      return "X";
   }
   else
   {
      return " ";
   }
}

不確定要執行的操作,但是如果您想要該方法的通用版本,而只改變結果,則可以將結果作為二維數組傳遞。

string method1(string[][] result) {
   bool x= Convert.ToBoolean(...);
   bool y= Convert.ToBoolean(...);
   int u = (x ? 1 : 0);
   int v = (y ? 1 : 0);
   return result[u][v];
}

這取決於實際問題,但是我懷疑您需要一個可以在任何地方重用的新類:

class DecisionMaker
{
    string _both;
    string _notX;
    string _notY;
    string _neither;
    string _default;

    public DecisionMaker(string both, string notX, string notY, string neither, string defaultVal)
    {
        _both = both;
        _notX = notX;
        _notY = notY;
        _neither = neither;
        _default = defaultVal;
    }

    public string Method1(bool x, bool y)
    {
        if (x && y)
            return _both;

        if (!x && y)
            return _notX;

        if (!y && x)
            return _notY;

        if (!x && !y)
            return _neither;

        return _default;
    }
}

或者(可選),您可以在Method1中使用Convert.ToBoolean(...)東西,並使Method1沒有參數。 或者,您可以創建帶有受保護抽象方法的DecisionMakerBase抽象類,該抽象方法僅允許您更改需要更改的功能。 這確實取決於您的情況。

如果您希望方法二針對四種不同的情況返回四個不同的值,則可以執行以下操作:

string method1(string[] values)
{
if (x && y)
 {
     return values[0];
 }

 if (!x && y)
 {
     return values[1];
  }

  if (!y && x)
  {
      return values[2];
  }

  if (!x && !y)
  {
      return values[3];
  }

}

但是,這不是很漂亮。 此方法在您的應用程序中有什么用?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM