-1
            WebClient wc = new WebClient();
            string code = wc.DownloadString("link");
            MessageBox.Show(code); // CODE SHOWS IN MESSAGEBOX CORRECTLY.
            if (textbox.Text == code)
            {
                MessageBox.Show("Key Approved!");
                try
                {
                    Form1 Form1 = new Form1();
                    Form1.Show();
                    this.Hide();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("This Key is incorrect.");
            }

The text inside the Textbox is the Text in the code string, although textbox.Text == code is false and it returns to the else argument.

Any idea why this is happening?

Pankwood
  • 1,799
  • 5
  • 24
  • 43
EngineEye
  • 11
  • 7
  • There may be leading or trailing whitespace in the returned string. – Jonathon Chase Nov 29 '19 at 20:06
  • i have checked, there are none. – EngineEye Nov 29 '19 at 20:06
  • We may need to see samples of strings you're comparing. There's also the possibility of zero-length characters being included. You should check that both of the strings you are comparing are of the same length. Other possibilities may be homoglyphs that appear to look the same, but are actually different characters. – Jonathon Chase Nov 29 '19 at 20:19
  • Because `textbox.Text` and `code` are not same string. It can look same when you idisplay it somewhere, but actual data is not a same. – Fabio Nov 29 '19 at 20:19
  • I suggest that you [create a hex dump](https://stackoverflow.com/q/16999604/87698) of both strings to compare them. – Heinzi Nov 29 '19 at 20:30

2 Answers2

0

Text inside the Textbox is the Text in the code string, although textbox.Text == code is false and it returns to the else argument.

I don't believe you. And since you failed to show evidence for this, I'm convinced you are wrong.

This suggests that TextBox.Text is not identical to code. If they look the same, then the difference is probably something like extra spaces, upper vs. lower case, or other minor differences.

The only other imaginable cause is that you've overridden the string equality operator somehow to do something unexpected.

Try this code instead:

Test(TextBox.Text, code);

void Test(string textbox, string code)
{
    if (textbox.Length != code.Length)
    {
        MessageBox.Show("Strings are different lengths!");
        return;
    }

    for (int i = 0; i < textbox.Length; i++)
    {
        if (textbox[i] != code[i])
        {
            MessageBox.Show(string.Format("'{0}' does not equal '{1}'", textbox[i], code[i]));
            return;
        }
    }
    MessageBox.Show("Strings are identical!");
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

Why not use .equals() instead of == when comparing strings