2011-07-14 87 views
1

這個問題對於回答或查看我以前關於break的問題的一些人可能看起來很熟悉;聲明。如果情況1得到滿足,我想做點什麼,如果情況2則要做其他事情。像下面這樣。任何人都可以指導我(如果可行的話),我可以如何實現我想要做的,而不必將我的if語句放在switch case中?c#:Switch case:if(case :)

 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 
     } 
+0

你想做什麼? – Tocco

+1

'int(Selenium.GetXpathCount(「// [@ id ='Results_Table']」)'?你是指'(int)Selenium.GetXpathCount(「// [@ id ='Results_Table']」)?? – BoltClock

+0

是的,感謝您糾正我 – Maya

回答

1
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語句添加的功能不符合您的需求?

5
if (searchType == "SearchAuthors") 

你不能做任何比這更好的。

+0

正如許多其他答案顯示,顯然你可以做得比這更好:) – abelenky

+0

但只有通過重新設計其餘的方法。 – SLaks

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

或者可能調用常用方法。 – Gebb

+0

@Gebb:如果公用線很複雜,或者多於一行,常用的方法可能會有所幫助。但是提供這樣簡單的方法調用會帶來更多的複雜性而不是刪除。 – abelenky

1

searchType將包含您需要的值。

所以,你可以有你目前的情況下發言,然後寫

if (searchType == "SearchBooks") 
// do something 
else if (searchType == "SearchAuthors") 
// do something else 
1
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); 
+0

謝謝......修復。不過,我想我已經得到了這個觀點。 –

2

你可以這樣做了許多不同的方式,這取決於你的理由不想包括在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 
    } 
} 
1

如果您爲了清晰起見而希望擁有兩批開關語句,請先將字符串轉換爲枚舉,然後從此處繼續。

如果您有興趣這樣做,我可以進一步填寫答案。讓我知道。

public enum SearchTypeEnum{None,SearchBooks,SearchAuthors} 

var searchType = (SearchTypeEnum)Enum.Parse(typeof(SearchTypeEnum), searchTypeString); 

switch (searchType){  
case SearchTypeEnum.SearchBooks: 
... 

switch (searchType){  
case SearchTypeEnum.SearchBooks: 
... 
4

打電話給我極客。 :)

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

並保存case語句。

現在,顯然,這隻適用於searchType值始終與實際元素ID相對應的情況。

+0

我們都在想它。 ;-) – StriplingWarrior

+0

Selenium.Type(string.Format(「{0} _TextInput」,searchType),searchText); Selenium.Click(string.Format(「{0} _SearchBtn」,searchType));也會起作用 –