Operator "!=" does not work in my code?

Asked

Viewed 163 times

0

I have a part of my program that I do several checks when the button is clicked and one of these checks is: it picks the value inserted in textboxRankTecnica and has to buy with the array ranksAceitaveis. If he enters a value that is not in ranksAceitaveis, he has to display the MessageBox. If it is the same, it continues with the code, but the big problem is that even typing an acceptable value, it launches the textbox. Codes I’ve tried (none of them did what they wanted, because even typing a correct value, it returns the message):

string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S", "S+", "S++" };
        else if (textBoxRankTecnica.Text.Length > 0)
        {
            for (int i = 0; i <= (ranksAceitaveis.Length - 1); i++)
            {
                if (!textBoxRankTecnica.Text.Contains(ranksAceitaveis[i]))
                {
                    passouCriacao = false;
                    MessageBox.Show("O Rank digitado é inválido [...]".", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                }
            }


        }

    /*outro codigo:*/ string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+" , "B", "B+", "A", "A+", "S", "S+", "S++"};

    else if (textBoxRankTecnica.Text != ranksAceitaveis.ToString().ToLowerInvariant())
        {
            passouCriacao = false;
            MessageBox.Show("O Rank inserido é inválido, digite um [...]".", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    /*outro codigo:*/ else if (textBoxRankTecnica.Text != "E".ToLowerInvariant() || textBoxRankTecnica.Text != "E+".ToLowerInvariant()) //E o resto das verificações

    /*outro codigo:*/ else if (textBoxRankTecnica.Text.Length > 0)
        {
            for (int i = 0; i <= (ranksAceitaveis.Length - 1); i++)
            {
                if (textBoxRankTecnica.Text != ranksAceitaveis[i].ToString().ToLowerInvariant())
                {
                    passouCriacao = false;
                    MessageBox.Show("Text [...]".", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }   
        }
  • 1

    Is this what you want? https://dotnetfiddle.net/5sJWKh

  • Nor was it for my case. Even if I enter a correct value (the ones in the variable "ranksAceitaveis") it continues with the same error. It’s boring this, huh e.e.g. I don’t know why any of these codes have worked so far . But still, thanks for trying to help.

  • 1

    Probably because you don’t know how to get what you want. Your question only has a few codes played, it is difficult to help so you need to define what should happen clearly before anything else. What’s more, based on the codes you posted that were written almost randomly, something tells me it didn’t work for you because it modified something it shouldn’t. The problem is just not well explained. This posted did not have the slightest chance of working because they do not make sense. If you don’t understand what you’re doing you won’t be able to program anything.

  • "Codes played" I believe the way I wrote generated it. They are not codes played, they are attempts. /another code/ means it’s another try, note the question that has "codes I’ve tried". The intention was to divide each attempt into a separate block, I couldn’t, so I did it by comment. Maybe I already have an idea of what might be going on and I’ll try to solve it.

  • I took your idea as a basis and adapted it to my needs and it worked. Thank you!

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

Show 1 more comment

3 answers

2

I believe you want to check if the value typed in the textbox is inside the array, correct?

Try something like that:

string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S", "S+", "S++" };
if (!string.IsNullOrEmpty(textBoxRankTecnica.Text) && ranksAceitaveis.Contains(textBoxRankTecnica.Text.ToUpper()))
{
    ...
}

Good luck!

2

The question was not so clear, the algorithm was quite wrong, but as the author solved the problem based on the comment, posted as an answer.

using static System.Console;
                    
public class Program {
    public static void Main() {
        var textBoxRankTecnica = "C+";
        string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S", "S+", "S++" };
        for (int i = 0; i < ranksAceitaveis.Length; i++) {
            if (textBoxRankTecnica == ranksAceitaveis[i]) {
                WriteLine("O Rank digitado é válido");
                break;
            }
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Could have been done with foreach, but I kept the line originally used.

1

There seems to be nothing wrong, but the green color shows that most of the code has become a string.

string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S", "S+", "S++" };
        else if (textBoxRankTecnica.Text.Length > 0)
        {
            for (int i = 0; i <= (ranksAceitaveis.Length - 1); i++)
            {
                if (!textBoxRankTecnica.Text.Contains(ranksAceitaveis[i]))
                {
                    passouCriacao = false;
                    MessageBox.Show("O Rank digitado é inválido [...]".", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                }
            }


        }

    /*outro codigo:*/ string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+" , "B", "B+", "A", "A+", "S", "S+", "S++"};

    else if (textBoxRankTecnica.Text != ranksAceitaveis.ToString().ToLowerInvariant())
        {
            passouCriacao = false;
            MessageBox.Show("O Rank inserido é inválido, digite um [...]".", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    /*outro codigo:*/ else if (textBoxRankTecnica.Text != "E".ToLowerInvariant() || textBoxRankTecnica.Text != "E+".ToLowerInvariant()) //E o resto das verificações

    /*outro codigo:*/ else if (textBoxRankTecnica.Text.Length > 0)
        {
            for (int i = 0; i <= (ranksAceitaveis.Length - 1); i++)
            {
                if (textBoxRankTecnica.Text != ranksAceitaveis[i].ToString().ToLowerInvariant())
                {
                    passouCriacao = false;
                    MessageBox.Show("Text [...]".", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }   
        }

Unintentionally (or without a head (no offence)) You put another quotation marks after the first parenthesis on line 9. This is the correct code:

string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+", "B", "B+", "A", "A+", "S", "S+", "S++" };
        else if (textBoxRankTecnica.Text.Length > 0)
        {
            for (int i = 0; i <= (ranksAceitaveis.Length - 1); i++)
            {
                if (!textBoxRankTecnica.Text.Contains(ranksAceitaveis[i]))
                {
                    passouCriacao = false;
                    MessageBox.Show("O Rank digitado é inválido [...].", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                }
            }


        }

    string[] ranksAceitaveis = new string[] { "E", "E+", "D", "D+", "C", "C+" , "B", "B+", "A", "A+", "S", "S+", "S++"};

    else if (textBoxRankTecnica.Text != ranksAceitaveis.ToString().ToLowerInvariant())
        {
            passouCriacao = false;
            MessageBox.Show("O Rank inserido é inválido, digite um [...].", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    else if (textBoxRankTecnica.Text != "E".ToLowerInvariant() || textBoxRankTecnica.Text != "E+".ToLowerInvariant()) //E o resto das verificações

    else if (textBoxRankTecnica.Text.Length > 0)
        {
            for (int i = 0; i <= (ranksAceitaveis.Length - 1); i++)
            {
                if (textBoxRankTecnica.Text != ranksAceitaveis[i].ToString().ToLowerInvariant())
                {
                    passouCriacao = false;
                    MessageBox.Show(Text "[...].", "Rank inválido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }   
        }

I had to take the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.