vendredi 18 janvier 2019

Is there a way I can reduce the number of if statements used?

So essentially I am trying to reduce the amount if if statements used.

I need the message to be specific to both certificate types. That's what's causing the problem. I could use || and && operators but then I would need the message to be a bit vague which i don't want.

Switch statements don't work because i'm using > and < I have tried using ? but couldn't get my head around it.

if (compatibilityCerts > 2 && nonCompCerts > 2)
            {
                rtbSummaryLog.AppendText(string.Format("There Are More Than Two Compatibility And Non-Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
                pctCertificateQuantity.BackColor = Color.Red;
                lblCertificateQuantity.Text = "More Than Two Compatibility And Non-Compatibility Certificates Detected";
            }
            else if (compatibilityCerts == 0 && nonCompCerts == 0)
            {
                rtbSummaryLog.AppendText(string.Format("There Are No Certificates On This Smart Card. {0}", Environment.NewLine));
                pctCertificateQuantity.BackColor = Color.Red;
                lblCertificateQuantity.Text = "No Certificates Detected";
            }
            else if (compatibilityCerts < 2 || nonCompCerts < 2)
            {
                rtbSummaryLog.AppendText(string.Format("There Are Certificates Missing On This Smart Card. {0}", Environment.NewLine));
                pctCertificateQuantity.BackColor = Color.Red;
                lblCertificateQuantity.Text = "No Certificates Detected";
            }
            else
            {
                if (compatibilityCerts > 2)
                {
                    rtbSummaryLog.AppendText(string.Format("There Are More Than Two Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
                    pctCertificateQuantity.BackColor = Color.Red;
                    lblCertificateQuantity.Text = "More Than Two Compatibility Certificates Detected";
                }
                else if (nonCompCerts > 2)
                {
                    rtbSummaryLog.AppendText(string.Format("There Are More Than Two Non-Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
                    pctCertificateQuantity.BackColor = Color.Red;
                    lblCertificateQuantity.Text = "More Than Two Non-Compatibility Certificates Detected";
                }
                else if (compatibilityCerts == 0)
                {
                    rtbSummaryLog.AppendText(string.Format("There Are No Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
                    pctCertificateQuantity.BackColor = Color.Red;
                    lblCertificateQuantity.Text = "No Compatibility Certificates Detected";
                }
                else if (nonCompCerts == 0)
                {
                    rtbSummaryLog.AppendText(string.Format("There Are No Non-Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
                    pctCertificateQuantity.BackColor = Color.Red;
                    lblCertificateQuantity.Text = "No Non-Compatibility Certificates Detected";
                }
                else
                {
                    pctCertificateQuantity.BackColor = Color.LightGreen;
                    lblCertificateQuantity.Text = "Number of Certificates is Valid";
                }

            }

Need to reduce if statements but maintain the specific messages.

Aucun commentaire:

Enregistrer un commentaire