簡體   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