1
\$\begingroup\$

I created a program that will convert a 3-character combo into a 3-string combo. I used a lot of if statements to accomplish the conversion. I'm wondering if there is any way to reduce the lines of code and possibly make it more efficient.

using System;
using System.Windows.Forms;

namespace Adow
{
    public partial class AdowForm : Form
    {
        private string FirstCharacter;
        private string SecondCharacter;
        private string ThirdCharacter;

        private string A = "0NFGSosLPI";
        private string B = "izNclMYrUO";
        private string C = "t7SjxAlxp5";
        private string D = "CeA7dwvVxY";
        private string E = "6kF4qi5bmt";
        private string F = "WuuQpKLN7n";
        private string G = "jy24ZxU7Ok";
        private string H = "8j0RAVF1Ka";
        private string I = "h9g173xKDV";
        private string J = "ypEeaBAzBx";
        private string K = "HNhqhtYMaU";
        private string L = "4qUvzJfvRA";
        private string M = "jJjAfk4m1y";
        private string N = "iF9d70yyf5";
        private string O = "9VlQkb0VKN";
        private string P = "nEdrSFCVTO";
        private string Q = "3ducfBmoi5";
        private string R = "atWIdmnsnG";
        private string S = "4edrAImSrq";
        private string T = "aUhZY1uDA5";
        private string U = "ARHxAVFnnN";
        private string V = "VKNXzjDlrx";
        private string W = "9eDu4vY24O";
        private string X = "tWAspyXsMM";
        private string Y = "DY89qcdHnP";
        private string Z = "SNSjmtyVtw";

        public AdowForm()
        {
            InitializeComponent();
        }

        private void ConvertButton_Click(object sender, EventArgs e)
        {
            //////////////////////////////////////////////////
            //                                              //
            //     FirstCharacter                           //
            //                                              //
            //////////////////////////////////////////////////

            if (FirstCharacterTextBox.Text.Equals("A", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = A;
            }
            else if (FirstCharacterTextBox.Text.Equals("B", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = B;
            }
            else if (FirstCharacterTextBox.Text.Equals("C", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = C;
            }
            else if (FirstCharacterTextBox.Text.Equals("D", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = D;
            }
            else if (FirstCharacterTextBox.Text.Equals("E", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = E;
            }
            else if (FirstCharacterTextBox.Text.Equals("F", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = F;
            }
            else if (FirstCharacterTextBox.Text.Equals("G", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = G;
            }
            else if (FirstCharacterTextBox.Text.Equals("H", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = H;
            }
            else if (FirstCharacterTextBox.Text.Equals("I", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = I;
            }
            else if (FirstCharacterTextBox.Text.Equals("J", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = J;
            }
            else if (FirstCharacterTextBox.Text.Equals("K", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = K;
            }
            else if (FirstCharacterTextBox.Text.Equals("L", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = L;
            }
            else if (FirstCharacterTextBox.Text.Equals("M", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = M;
            }
            else if (FirstCharacterTextBox.Text.Equals("N", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = N;
            }
            else if (FirstCharacterTextBox.Text.Equals("O", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = O;
            }
            else if (FirstCharacterTextBox.Text.Equals("P", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = P;
            }
            else if (FirstCharacterTextBox.Text.Equals("Q", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = Q;
            }
            else if (FirstCharacterTextBox.Text.Equals("R", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = R;
            }
            else if (FirstCharacterTextBox.Text.Equals("S", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = S;
            }
            else if (FirstCharacterTextBox.Text.Equals("T", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = T;
            }
            else if (FirstCharacterTextBox.Text.Equals("U", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = U;
            }
            else if (FirstCharacterTextBox.Text.Equals("V", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = V;
            }
            else if (FirstCharacterTextBox.Text.Equals("W", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = W;
            }
            else if (FirstCharacterTextBox.Text.Equals("X", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = X;
            }
            else if (FirstCharacterTextBox.Text.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = Y;
            }
            else if (FirstCharacterTextBox.Text.Equals("Z", StringComparison.InvariantCultureIgnoreCase))
            {
                FirstCharacter = Z;
            }

            //////////////////////////////////////////////////
            //                                              //
            //     SecondCharacter                          //
            //                                              //
            //////////////////////////////////////////////////

            if (SecondCharacterTextBox.Text.Equals("A", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = A;
            }
            else if (SecondCharacterTextBox.Text.Equals("B", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = B;
            }
            else if (SecondCharacterTextBox.Text.Equals("C", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = C;
            }
            else if (SecondCharacterTextBox.Text.Equals("D", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = D;
            }
            else if (SecondCharacterTextBox.Text.Equals("E", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = E;
            }
            else if (SecondCharacterTextBox.Text.Equals("F", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = F;
            }
            else if (SecondCharacterTextBox.Text.Equals("G", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = G;
            }
            else if (SecondCharacterTextBox.Text.Equals("H", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = H;
            }
            else if (SecondCharacterTextBox.Text.Equals("I", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = I;
            }
            else if (SecondCharacterTextBox.Text.Equals("J", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = J;
            }
            else if (SecondCharacterTextBox.Text.Equals("K", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = K;
            }
            else if (SecondCharacterTextBox.Text.Equals("L", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = L;
            }
            else if (SecondCharacterTextBox.Text.Equals("M", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = M;
            }
            else if (SecondCharacterTextBox.Text.Equals("N", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = N;
            }
            else if (SecondCharacterTextBox.Text.Equals("O", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = O;
            }
            else if (SecondCharacterTextBox.Text.Equals("P", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = P;
            }
            else if (SecondCharacterTextBox.Text.Equals("Q", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = Q;
            }
            else if (SecondCharacterTextBox.Text.Equals("R", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = R;
            }
            else if (SecondCharacterTextBox.Text.Equals("S", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = S;
            }
            else if (SecondCharacterTextBox.Text.Equals("T", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = T;
            }
            else if (SecondCharacterTextBox.Text.Equals("U", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = U;
            }
            else if (SecondCharacterTextBox.Text.Equals("V", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = V;
            }
            else if (SecondCharacterTextBox.Text.Equals("W", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = W;
            }
            else if (SecondCharacterTextBox.Text.Equals("X", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = X;
            }
            else if (SecondCharacterTextBox.Text.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = Y;
            }
            else if (SecondCharacterTextBox.Text.Equals("Z", StringComparison.InvariantCultureIgnoreCase))
            {
                SecondCharacter = Z;
            }

            //////////////////////////////////////////////////
            //                                              //
            //     ThirdCharacter                           //
            //                                              //
            //////////////////////////////////////////////////

            if (ThirdCharacterTextBox.Text.Equals("A", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = A;
            }
            else if (ThirdCharacterTextBox.Text.Equals("B", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = B;
            }
            else if (ThirdCharacterTextBox.Text.Equals("C", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = C;
            }
            else if (ThirdCharacterTextBox.Text.Equals("D", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = D;
            }
            else if (ThirdCharacterTextBox.Text.Equals("E", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = E;
            }
            else if (ThirdCharacterTextBox.Text.Equals("F", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = F;
            }
            else if (ThirdCharacterTextBox.Text.Equals("G", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = G;
            }
            else if (ThirdCharacterTextBox.Text.Equals("H", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = H;
            }
            else if (ThirdCharacterTextBox.Text.Equals("I", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = I;
            }
            else if (ThirdCharacterTextBox.Text.Equals("J", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = J;
            }
            else if (ThirdCharacterTextBox.Text.Equals("K", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = K;
            }
            else if (ThirdCharacterTextBox.Text.Equals("L", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = L;
            }
            else if (ThirdCharacterTextBox.Text.Equals("M", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = M;
            }
            else if (ThirdCharacterTextBox.Text.Equals("N", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = N;
            }
            else if (ThirdCharacterTextBox.Text.Equals("O", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = O;
            }
            else if (ThirdCharacterTextBox.Text.Equals("P", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = P;
            }
            else if (ThirdCharacterTextBox.Text.Equals("Q", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = Q;
            }
            else if (ThirdCharacterTextBox.Text.Equals("R", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = R;
            }
            else if (ThirdCharacterTextBox.Text.Equals("S", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = S;
            }
            else if (ThirdCharacterTextBox.Text.Equals("T", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = T;
            }
            else if (ThirdCharacterTextBox.Text.Equals("U", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = U;
            }
            else if (ThirdCharacterTextBox.Text.Equals("V", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = V;
            }
            else if (ThirdCharacterTextBox.Text.Equals("W", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = W;
            }
            else if (ThirdCharacterTextBox.Text.Equals("X", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = X;
            }
            else if (ThirdCharacterTextBox.Text.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = Y;
            }
            else if (ThirdCharacterTextBox.Text.Equals("Z", StringComparison.InvariantCultureIgnoreCase))
            {
                ThirdCharacter = Z;
            }

            MessageBox.Show(FirstCharacter + " " + SecondCharacter + " " + ThirdCharacter);
        }
    }
}

Adow

Convert

\$\endgroup\$
6
\$\begingroup\$

You need to capitalise on the concept of better storage methods than a massive amount of strings.

Consider using a Dictionary instead:

    private string A = "0NFGSosLPI";
    private string B = "izNclMYrUO";

Into:

private Dictionary<string, string> letters = new Dictionary<string, string>()
{
    { "A", "0NFGSosLPI" },
    { "B", "izNclMYrUO" },

    // ...
};

However, I would still recommend moving this to another file to condense your code in this file, and in case you ever needed to call this Dictionary from another file.

And then, instead of your massive if-else loop, you can use a simple Dictionary lookup:

FirstCharacter = Letters[FirstCharacterTextBox.Text];

Additionally, instead of direct string concatenation, I'd recommend using string.Format instead, this can give you better control in more advanced situations:

MessageBox.Show(FirstCharacter + " " + SecondCharacter + " " + ThirdCharacter);

into:

MessageBox.Show(string.Format("{0} {1} {2}", FirstCharacter, SecondCharacter, ThirdCharacter);

You can even customise this format to make more advanced formats:

string.Format("Hi, {0}, we've received your payment of {1} on {2}. Thanks!", customer.Name, payment.Amount, payment.Date);

Effectively leaving you with:

using System;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Adow
{
    public partial class AdowForm : Form
    {
        private string FirstCharacter;
        private string SecondCharacter;
        private string ThirdCharacter;
        private Dictionary<string, string> letters = new Dictionary<string, string>()
        {
            { "A", "0NFGSosLPI" },
            { "B", "izNclMYrUO" },
            { "C", "t7SjxAlxp5" },
            { "D", "CeA7dwvVxY" },
            { "E", "6kF4qi5bmt" },
            { "F", "WuuQpKLN7n" },
            { "G", "jy24ZxU7Ok" },
            { "H", "8j0RAVF1Ka" },
            { "I", "h9g173xKDV" },
            { "J", "ypEeaBAzBx" },
            { "K", "HNhqhtYMaU" },
            { "L", "4qUvzJfvRA" },
            { "M", "jJjAfk4m1y" },
            { "N", "iF9d70yyf5" },
            { "O", "9VlQkb0VKN" },
            { "P", "nEdrSFCVTO" },
            { "Q", "3ducfBmoi5" },
            { "R", "atWIdmnsnG" },
            { "S", "4edrAImSrq" },
            { "T", "aUhZY1uDA5" },
            { "U", "ARHxAVFnnN" },
            { "V", "VKNXzjDlrx" },
            { "W", "9eDu4vY24O" },
            { "X", "tWAspyXsMM" },
            { "Y", "DY89qcdHnP" },
            { "Z", "SNSjmtyVtw" }
        };

        public AdowForm()
        {
            InitializeComponent();
        }

        private void ConvertButton_Click(object sender, EventArgs e)
        {
            FirstCharacter  = Letters[FirstCharacterTextBox.Text.ToUpperInvariant()];
            SecondCharacter = Letters[SecondCharacterTextBox.Text.ToUpperInvariant()];
            ThirdCharacter  = Letters[ThirdCharacterTextBox.Text.ToUpperInvariant()];

            MessageBox.Show(string.Format("{0} {1} {2}", FirstCharacter, SecondCharacter, ThirdCharacter);
        }
    }
}

But, you should also do a Letters.ContainsKey check to make sure that the letter you're trying to look up is in the dictionary.

\$\endgroup\$
  • \$\begingroup\$ This doesn't do quite the same thing as the original code, as the original code works regardless of casing (yours only works on uppercase letters), and your dictionary lookup will throw an exception if not found. \$\endgroup\$ – 404 Jan 16 '16 at 11:52
  • \$\begingroup\$ @eurotrash is right. Your ConvertButton_Click method should be corrected along the lines of FirstCharacterTextBox.Text should be FirstCharacterTextBox.Text.ToUpperInvariant(), and so on. \$\endgroup\$ – Rick Davin Jan 16 '16 at 18:08
  • \$\begingroup\$ Ah, thanks for informing me, I'll fix it ASAP :-) \$\endgroup\$ – Quill Jan 17 '16 at 1:04
  • \$\begingroup\$ @Quill Thank you very much for the explanatory answer. \$\endgroup\$ – Owen Jan 18 '16 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.