【问题标题】:c#: Switch case: if (case:)c#: switch case: if (case:)
【发布时间】:2011-07-14 18:14:34
【问题描述】:

对于回答或查看过我之前关于休息的问题的一些人来说,这个问题可能看起来很熟悉;陈述。如果满足案例 1,我想做一些事情,如果满足案例 2,我想做点别的事情。如下所示。任何人都可以指导我(如果可行的话)如何实现我想要做的事情,而不必将我的 if 语句放在 switch 案例中?

        switch (searchType)
        {
            case "SearchBooks":
                Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
                break;

            case "SearchAuthors":
                Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
                break;
        }

        int count = int(Selenium.GetXpathCount("//[@id='Results_Table']");

        if case was "SearchBooks"
        {
            //do something
        }
        else if case was "SearchAuthors"
        {
            //do something else
        }

【问题讨论】:

  • int(Selenium.GetXpathCount("//[@id='Results_Table']")?你的意思是(int) Selenium.GetXpathCount("//[@id='Results_Table']")
  • 是的,感谢您的指正。

标签: c# selenium switch-statement


【解决方案1】:
if (searchType == "SearchAuthors")

没有比这更好的了。

【讨论】:

  • 正如这里的许多其他答案所示,显然您可以做得更好。 :)
  • 但只能通过重新设计其余的方法。
【解决方案2】:

叫我极客。 :)

Selenium.Type(string.Format(@"//*[@id='{0}_TextInput']", searchType), searchText);
Selenium.Click(string.Format(@"//*[@id='{0}_SearchBtn']", searchType));

并保存案例陈述。

现在,显然,这仅在 searchType 值始终对应于实际元素 ID 时才有效。

【讨论】:

  • Selenium.Type(string.Format("{0}_TextInput", searchType), searchText); Selenium.Click(string.Format("{0}_SearchBtn", searchType));也会起作用
【解决方案3】:

如果这两种情况的唯一共同点是

int count = int(Selenium.GetXpathCount("//[@id='Results_Table']");

那我就写两遍。

int count;
switch (searchType)
{
    case "SearchBooks":
        Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
        count = int(Selenium.GetXpathCount("//[@id='Results_Table']");
        //do something
        break;

    case "SearchAuthors":
        Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
        count = int(Selenium.GetXpathCount("//[@id='Results_Table']");
        //do something else
        break;
}

【讨论】:

  • 或者调用一个常用的方法。
  • @Gebb:如果公共线很复杂,或者不止一条线,那么一种公共方法可能会有所帮助。但是如果它像这样简单,方法调用引入的复杂性比它消除的复杂性要多。
【解决方案4】:

您可以通过多种不同的方式执行此操作,具体取决于您不想在 case 语句中包含代码的原因。其他回答者有很好的建议。在我看来,这段代码在呼唤面向对象的方法:

var search = GetSearch(searchType);
search.PerformSearch(searchText);
int count = (int)Selenium.GetXpathCount("//[@id='Results_Table']");
search.DoSomething(count);

...

public ISearch GetSearch(string searchType)
{
    switch (searchType)
    {
        case "SearchBooks": return new SearchBooks();
        case "SearchAuthors": return new SearchAuthors();
        default: 
            throw new ArgumentException(
                string.Format("Invalid searchtype \"{0}\"", searchType), 
                "searchType");
    }
}


public interface ISearch
{
    void PerformSearch(string searchText);
    void DoSomething();
}

public class SearchBooks : ISearch
{
    public void PerformSearch(string searchText)
    {
        Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
    }

    void DoSomething()
    {
        // do something
    }
}

public class SearchAuthors : ISearch
{
    public void PerformSearch(string searchText)
    {
        Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
    }

    void DoSomething()
    {
        // do something else
    }
}

【讨论】:

    【解决方案5】:
        public enum MyEnumSearchTypes
        {
            SearchBooks, SearchAuthors, SearchSomethingElse 
        }
    
        public void MyDoSomethingMethod(MyEnumSearchTypes searchType)
        {
            switch (searchType)
            {
                case MyEnumSearchTypes.SearchBooks:
                    Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
                    Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
                break;
    
                case MyEnumSearchTypes.SearchAuthors:
                    Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
                    Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
                break;
            }    
    
            if (searchType == MyEnumDefinedTypes.SearchBooks)
            {     
               do something
            }       
    
            else if (searchType == MyEnumDefinedTypes.SearchAuthors)
            {     
               do something else
            }            
    
            else
            {     
               do nothing
            }     
        }        
    

    但是,我不明白为什么在 switch 语句中添加功能不符合您的需求?

    【讨论】:

      【解决方案6】:

      searchType 将包含您需要的值。

      所以你可以有你当前的案例陈述,然后写

      if (searchType == "SearchBooks")
      // do something
      else if (searchType == "SearchAuthors")
      // do something else
      

      【讨论】:

        【解决方案7】:
            Action<int> myAction;
            switch (searchType)
                {
                    case "SearchBooks":
                        Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
                        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
                        // assign custom action
                        myAction = count => { /* the count is count */};
                        break;
        
                    case "SearchAuthors":
                        Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
                        Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
                        // assign custom action
                        myAction = count => { /* the count is count */};
                        break;
                   default:
                      throw new NotSupportedException ();
                }
        
                int count = int(Selenium.GetXpathCount("//[@id='Results_Table']");
                // invoke custom action
                myAction(count);
        

        【讨论】:

          【解决方案8】:

          如果为了清楚起见,您希望有两个大量的 switch 语句,首先将字符串转换为枚举,然后从那里继续。

          如果您有兴趣这样做,我可以进一步填写答案。告诉我。

          public enum SearchTypeEnum{None,SearchBooks,SearchAuthors}
          
          var searchType = (SearchTypeEnum)Enum.Parse(typeof(SearchTypeEnum), searchTypeString);
          
          switch (searchType){    
          case SearchTypeEnum.SearchBooks:
          ...
          
          switch (searchType){    
          case SearchTypeEnum.SearchBooks:
          ...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-16
            • 1970-01-01
            • 2013-09-10
            • 2014-07-06
            • 1970-01-01
            相关资源
            最近更新 更多