Convertendo e Validando dados Text para int no C#

Em certos casos de uma interface de usuário onde é preciso verificar se o campo é numérico ou não, use o TryParse. veja o código a seguir onde a variável qtde (do tipo int) está referenciada no out do TryParse. ele retorna true/false e se converter, beleza; caso contrário vc trata.

int qtde; 
if (int.TryParse(txtQtde.Text.Trim(), out qtde) == false)
{
     MessageBox.Show("mensagem")
     return;

}

O VB.NET também aceita isto, mas o VB.NET tem o IF (IsNumeric(txtQtde.Text)... Bendito VB.NET :)

2 Comments

  • A mutual friend stated I must come take a look at your site. Glad I clicked around the website link! I like how you really focus and get to your bottom line but can you operate by way of that last portion again? Just just a little?

    --------------------------------------------------------------------
    Biology, Organismic and Evolutionary

  • "Have you ever considered including far more movies in your blog site posts to hold the readers far more entertained? I imply I just study via the whole article of yours and it was fairly very good but considering that I am additional of a visual learner,I observed that to be far more helpful. Just my my thought, Beneficial luck "

    --------------------------------------------------------------------
    Earth and Planetary Sciences

Comments have been disabled for this content.