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

Shorten redundant switch statements

I got this block of code from a solution I google for a problem I was searching regarding spaghetti if..else statements. Is there a way to shorten this at all or a different approach to make the code more maintainable atleast.

            switch (registerControl.Valid_FullName(student.Student_Name) == true)
            {
                case true:
                    lblFullNameError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblFullNameError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Valid_Email(student.Student_Email) == true)
            {
                case true:
                    lblEmailError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblEmailError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Valid_Course(student.Student_Course) == true)
            {
                case true:
                    lblCourseError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblCourseError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Valid_Password(student.Student_Password) == true)
            {
                case true:
                    lblPasswordError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblPasswordError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Confirm_Password(student.Student_Password, student.student_ConfirmPassword) == true)
            {
                case true:
                    lblPasswordMatch.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblPasswordMatch.Visibility = Visibility.Visible;
                    break;
            }

>Solution :

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

You can convert your switch statement to a one-liner, using ternary operator,

lblFullNameError.Visibility = registerControl.Valid_FullName(student.Student_Name)  
                  ? Visibility.Hidden : Visibility.Visible;

You can apply same logic for rest of the switch blocks.

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