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

How can I reduce the no of IFs

How can I reduce the no of IF statements and rewrite the same efficiently.

Name is dynamic. Name could be "One hundred" or "one thousand". It can "Two hundred" or "Two thousand". I want it to enter the respective IF if Name contains the word ‘one’ or ‘two’.

ID and Name contain same string. ID will also hold "One hundred" or "one thousand"…

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

         if (Name.Contains("One"))
         {
             var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index &&  f.Id.Contains("One"));                  
             axisIndex = AddAxis(checkIndex, axis);               
         }
         else if (Name.Contains("Two"))
         {
             var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains("Two"));
             axisIndex = AddAxis(checkAxisIndex, axis);
         }
         else if (Name.Contains("Three"))
         {
             var checkIndex = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains("Three"));
             axisIndex = AddAxis(checkAxisIndex, axis);
         }
         .....
         else     
         {
            ....
         }

Thank you.

>Solution :

You can use an approach like this :

var possibleNames = new []{"One", "Two", "Three", "Four"};
var selectedName = possibleNames.FirstOrDefault(n=> Name.Contains(n));

if(string.IsNullOrWhiteSpace(selectedName)) // return or throw

var index = Chart.YAxis.FindIndex(f => f.Index == index && f.Id.Contains(selectedName));
axisIndex = AddAxis(index , axis)
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