How to make an if-type validation in a set of options?

Asked

Viewed 273 times

-1

I need to make a if where I check a set of options not to do a if within another if to check whether a field is filled or not, I want to do this with a single if. Ex if(valr1, valor2, valor3 != "")

Follows the code

DataSet ds = new DataSet();

ds.ReadXml(@"\\192.168.0.251\Geral_G.M\Expedicao\XML_ENTRADA\" + txt_chave.Text + ".xml");

txt_fornecedor.Text   = ds.Tables["emit"].Rows[0]["xNome"].ToString();
txt_cnpj.Text         = ds.Tables["emit"].Rows[0]["CNPJ"].ToString();
txt_nota.Text         = ds.Tables["ide"].Rows[0]["nNF"].ToString();
txt_ie.Text           = ds.Tables["emit"].Rows[0]["IE"].ToString();
emissao               = Convert.ToDateTime(ds.Tables["ide"].Rows[0]["dhEmi"]);
saida                 = Convert.ToDateTime(ds.Tables["ide"].Rows[0]["dhSaiEnt"]);

txt_emissao.Text      = Convert.ToString(emissao);
txt_saida.Text        = Convert.ToString(saida);
  • 1

    the code does not seem to be related to the question. Can elaborate an example?

  • Rovann and this same code, because I’m reading an xml, and I play the value inside the textbox, I just need to confirm if the values are coming from xml and filling the textbox, because I noticed that some xmls come without one of the information I need to fill the textbox.

  • Poe example I have received some xlms without the output date, I need to confirm this and give a message to the user that this missing given in the read xml.

  • I need to confirm the information in this block that I am receiving.

  • Still confused, can you be more specific? I mean that the columns that are in the code (for example "xName") is what you want to validate? Do you want to know if all are filled in a single if?

  • Ricardo that’s right, I need to validate the information that is coming from xml.

  • 2

    Seems more like a logic problem you’re using. In addition, NF-e provides the schematics in XSD, you can generate the classes and directly convert the xml into an object. The way you’re doing it gets more complicated...

  • I get it, I’ll improve this code yes, thanks for the tip.

Show 3 more comments

2 answers

3


In your case I suggest you create a class with the fields that represent this XML and use Dataannotations for properties that cannot be empty, would be the attribute Required, see:

[Required(ErrorMessage = "Nome é obrigatório.")]
public string Nome {get; set;}

It is a good alternative for validation of objects, can even implement the interface IValidatableObject on the object to be validated.

Using a list

If you want to check multiple values at once, you can use a list for this:

var valores = new List<string>{ "Hi", "casa", "Teste", "ga", "     sa ", null};    

In the method Exists you specify the predicate with the condition, which in case would be:

v => string.IsNullOrWhiteSpace(v)

To check if there is any null in the range null, emptiness "" or with white space " ".

See the full code:

using static System.Console;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {                
        var valores = new List<string>{ "Hi", "casa", "Teste", "ga", "     sa ", null};    
        var algumElementoVazioOuNulo = valores.Exists(v => string.IsNullOrWhiteSpace(v));
        if (algumElementoVazioOuNulo)
        {
            WriteLine("Sim");    
        }                
        else 
        {
            WriteLine("Nao");
        }
    }
}

See working on .NET Fiddle.

Learn more in documentation.

1

You can use logical operators && (and) or || (or), but you still have to do the individual checking of each item. You can try something like:

if(val1 != "" && val2 != "" && val3 != ""){ ... }

Then just follow the truth table inserir a descrição da imagem aqui

Browser other questions tagged

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