How to implement validation for negative values

前端 未结 1 1660
轻奢々
轻奢々 2021-01-28 10:06

I was working on this code snippet

  private void calculateButton_Click(object sender, EventArgs e)
    {
        int amount;
        if (int.TryParse(amountText         


        
1条回答
  •  有刺的猬
    2021-01-28 10:19

    Store the value of TryParse result, then check the amount in the same if statement, like so:

    boolean parseResult = int.TryParse(amountTextBox.Text, out amount)
    
    if (parseResult && amount >= 0)
    {
        //....
    }
    else
    {
        MessageBox.Show("Invalid amount");
    }
    

    0 讨论(0)
提交回复
热议问题