How to fill Textboxes?

Asked

Viewed 381 times

0

I wouldn’t want to use several if's to control a method of mine, unless there is no way to do it without the if's. I write in a list, the return of the BD that may or may not have banking information. The list returns me, Nome_Banco, Conta and Agencia. I need to fill out some textbox, but only if there is data. I’m having difficulties to do 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 if data exists or not. I can do it with if's and test if it is null or not, but the question is: Is there a way to do this without the if?


Responding to the Maniero.

These fields are displayed in a panel, like a "popup". These fields are in this panel. When in the form I type the CPF or CNPJ, then the panel is visible and these fields are filled after the search in the BD. The back list: CPF/CNPJ, Social Reason, Agency, Account, Bank Number and Bank Name(Institution).

Well, it turns out, since it’s an old rap sheet, I don’t always have the banking information. So when the panel is displayed (Visible), if any banking data comes null, will give error when assigning value to TextBoxes relative.

I’d like to avoid that mistake by already testing null in the information. if it comes null, the panel is displayed (Visible) with CPF/CNPJ and Social Reason filled and the other textboxes should come blank (empty) to be able to be filled manually(user type fields, agency, account, number and name of the bank (institution)).

  • And what do you want to do if it’s null?

  • It won’t fill out automatically. Then the padding will be manual, make the panel visible with the blank fields and then the user fills them manually.

  • But you have to explain how you want to proceed with this. Your questions are complicated because we have to guess how you are doing. What is manual padding? What is visible panel? You don’t mention any of this in the question. Which fields should be blank? What is it to be blank to you? I ask this because you usually say one thing but want another. Is there a problem when you do?

  • I will edit the question and try to be more explicit, but I think the question is appropriate.

1 answer

2


If I understand correctly, you can avoid the if properly said but can not avoid a decision , so I will use a ternary operator (I’ll put only what matters, then you replicate to the other fields):

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

So if the die is null he catches a string empty, otherwise take the die.

Are you sure you need the ToString()? If you don’t need it, you can simplify this by using the null-coalescing. Doesn’t seem to need.

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

An alternative would be to create an auxiliary method. Quick thinking could be a generic extension method that turns into string anything that received (gives p/ do even better than this), something like that:

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

I put in the Github for future reference.

Use:

txtAgenciaCadFornecedor.Text = vlstCorpFornecedor[0].Agencia.Coalesce();
  • I just tested this one and it worked. txtAgenciaCadFornecedor.Text = vlstCorpFornecedor[0]. Agency ?? "";

Browser other questions tagged

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