繁体   English   中英

c#: 开关盒: if (case:)

[英]c#: Switch case: if (case:)

回答或查看过我之前关于休息的问题的某些人可能会觉得这个问题很熟悉; 陈述。 如果满足案例 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
        }
if (searchType == "SearchAuthors")

你不能做得比这更好。

叫我极客。 :)

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

并保存案例陈述。

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

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

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

您可以通过多种不同的方式执行此操作,具体取决于您不想在 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
    }
}

searchType 将包含您需要的值。

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

if (searchType == "SearchBooks")
// do something
else if (searchType == "SearchAuthors")
// do something else
    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);
    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 语句中添加功能不符合您的需求?

如果为了清楚起见,您更喜欢使用两个大量的 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:
...

暂无
暂无

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

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