Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a code part where I use switch statement for defining the behaivours case by case. Is it possible to write this code without switch statement?

I have a c# app where I have to show elements of the correct list regarding the selection of the user. There are several lists and related to the selection of the user I have show the correct list on the screen.

Additionally I have to make some other process on list, regarding the selection.

I have used a switch statement for selecting the correct list but, because there are a lot of cases the code gets longer and longer. How can I shorten my code without using a switch stement? Or when switch is needed: Is it possible to use the case part in a for loop to shorten it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public void Split_List()
{
switch (TagService.NC_GUD_selected_type[index_])
        {

            case "SGUD": 
                for (int m = 0; m < SGUD_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = SGUD_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "MGUD":
                for (int m = 0; m < MGUD_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = MGUD_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "UGUD":
                for (int m = 0; m < UGUD_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = UGUD_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "GUD4":
                for (int m = 0; m < GUD4_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = GUD4_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "GUD5":
                for (int m = 0; m < GUD5_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = GUD5_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "GUD6":
                for (int m = 0; m < GUD6_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = GUD6_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "GUD7":
                for (int m = 0; m < GUD7_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = GUD7_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "GUD8":
                for (int m = 0; m < GUD8_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = GUD8_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;
            case "GUD9":
                for (int m = 0; m < GUD9_List_from_TreeWriter.LongCount(); m++)
                {
                    Split_GUD_attr = GUD9_List_from_TreeWriter[m].Split(";");
                    k = m;
                }
                break;

                // possible other cases
        }

        GUD_List_to_screen = Split_GUD_attr[0] + Split_GUD_attr[6] + "  (" + Split_GUD_attr[1] + ")";
        CNC_GUD_Out_Array_total[index_, k] = GUD_List_to_screen;
}

>Solution :

To shorten your code, you can create a dictionary that maps each selection to its corresponding list.

public void Split_List()
{
    Dictionary<string, List<string>> listDictionary = new Dictionary<string, List<string>>()
    {
        { "SGUD", SGUD_List_from_TreeWriter },
        { "MGUD", MGUD_List_from_TreeWriter },
        { "UGUD", UGUD_List_from_TreeWriter },
        { "GUD4", GUD4_List_from_TreeWriter },
        { "GUD5", GUD5_List_from_TreeWriter },
        { "GUD6", GUD6_List_from_TreeWriter },
        { "GUD7", GUD7_List_from_TreeWriter },
        { "GUD8", GUD8_List_from_TreeWriter },
        { "GUD9", GUD9_List_from_TreeWriter }
        // Add other cases here
    };

    if (listDictionary.TryGetValue(TagService.NC_GUD_selected_type[index_], out List<string> selectedList))
    {
        for (int m = 0; m < selectedList.Count; m++)
        {
            Split_GUD_attr = selectedList[m].Split(";");
            k = m;

            GUD_List_to_screen = Split_GUD_attr[0] + Split_GUD_attr[6] + "  (" + Split_GUD_attr[1] + ")";
            CNC_GUD_Out_Array_total[index_, k] = GUD_List_to_screen;
        }
    }
}

To add new cases, you can simply extend the dictionary with the new selection and its associated list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading