How to fill in TextBoxes?

0

I would not like to use multiple if's to control a method of mine, unless there is no way to do it without if's . Record in a list, the return of the DB that may or may not have bank information. The list returns me, Nome_Banco , Conta and Agencia . I need to fill in textbox , but only if there is data. I'm having trouble doing it without using if's . See how it is currently.

if (vlstCorpFornecedor.Count > 0)
        {
            //string nm_banco = vlstCorpFornecedor[0].Banco.ToString();
            txtCpfCnpjCadFornecedor.Text = vlstCorpFornecedor[0].NuCPFCNPJ.ToString();
            txtNomeRazaoSocialCadFornecedor.Text = vlstCorpFornecedor[0].Nome.ToString();
            txtAgenciaCadFornecedor.Text = vlstCorpFornecedor[0].Agencia.ToString();
            txtContaCadFornecedor.Text = vlstCorpFornecedor[0].ContaCorrente.ToString();
            cmbBancos.SelectedValue = vlstCorpFornecedor[0].Banco.ToString();
            cmbBancos_SelectedIndexChanged(this, EventArgs.Empty);
        }   

Where vlstCorpFornecedor is my list. From here txtAgenciaCadFornecedor and I can only fill in whether or not data exists. I can do with if's and test whether it is null or not, but the question is: Is there a way to do this without if ?

Responding to Maniero.

These fields are displayed in a panel, type a "popup". These fields are in this panel. When in the form I type the CPF or CNPJ, there the panel is visible and these fields are filled after the search in the database. The list below: CPF / CNPJ, Corporate Name, Agency, Account, Bank Number and Name of Bank (Institution).

Well, it happens, since this is an old registry, I do not always have the banking information. So when the panel is displayed (Visible), if any bank data come null , it will give error when assigning value to TextBoxes relative.

I would like to avoid this error by already testing null in the information. if it is null, the panel is displayed (Visible) with CPF / CNPJ and Social Ratio completed and the other textboxes must be blank (empty) to be filled manually (user types the fields, agency, account, number and name of the bank (institution)).

    
asked by anonymous 30.12.2014 / 16:15

1 answer

1

If I understand correctly you can avoid if itself but you can not avoid a decision, so I'll use a ternary operator (I'll just put what matters, then you reply to the other fields):

txtAgenciaCadFornecedor.Text = vlstCorpFornecedor[0].Agencia == null ? 
                                   "" : 
                                   vlstCorpFornecedor[0].Agencia.ToString();

So if the data is null it takes an empty string , otherwise it gets the data.

Are you sure you need ToString() ? If you do not need it, you can simplify this by using the null-coalescing operator. You do not seem to need it.

txtAgenciaCadFornecedor.Text = vlstCorpFornecedor[0].Agencia ?? "";

An alternative would be to create an auxiliary method. Thinking fast could be a generic extension method that would turn anything string into something (give it to do even better), something like this:

public static string Coalesce<T>(this T obj, string defaultValue = "") {
    if (obj == null)
        return defaultValue;
    return obj.ToString();
}

Usage:

txtAgenciaCadFornecedor.Text = vlstCorpFornecedor[0].Agencia.Coalesce();
    
30.12.2014 / 17:05