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

2

I have a part of my program that does several checks when the button is clicked and one of these checks is: it takes the value entered in textboxRankTecnica and has to buy with the array ranksAceitaveis . If it enters a value that is not in ranksAceitaveis , it must display MessageBox . If it is the same, it continues with the code, but the big problem is that even typing an acceptable value, it throws textbox . Codes that I have already 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);
                }
            }   
        }
    
asked by anonymous 26.02.2017 / 00:20

2 answers

1

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

Try something like this:

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!

    
27.02.2017 / 05:15
1

The question was not so clear, the algorithm was quite wrong, but as the author solved the problem based on the comment, I posted the 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;
            }
        }

    }
}

See running on .NET Fiddle . And at Coding Ground . Also I placed it on GitHub for future reference . It could have been done with foreach , but kept the line originally used.

    
01.03.2017 / 12:51