简体   繁体   中英

c#: Switch case: if (case:)

This question might seem familiar to some of you who answered or viewed my previous question regarding the break; statement. I want to do something if case 1 was satisfied, and something else if case 2. Something like the following. Can anyone guide me (if it is doable) on how I can achieve what I'm trying to do instead of having to put my if statements inside the switch 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
        }
if (searchType == "SearchAuthors")

You can't do any better than that.

Call me geek. :)

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

And save the case statements.

Now, obviously, this only works if searchType values always correspond to actual element IDs.

If the only line in common both cases is the

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

then I would just write that twice.

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

You could do this a number of different ways, depending on your reason for not wanting to include the code in the case statement. The other answerers have very good suggestions. It seems to me that this code is screaming for an object-oriented approach:

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 will contain the value of what you need.

So you can have your current case statement and then write

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

However, i don't get why adding the functionality in the the switch statement does not fit your needs?

If you prefer to have two lots of switch statements for clarity, first Cast the string to an enum and then proceed from there.

I can pad out the answer further if you are interested in doing it this way. Let me know.

public enum SearchTypeEnum{None,SearchBooks,SearchAuthors}

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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