Aaraadhana
Aaraadhana

Reputation: 145

How to check the datatype

I have a textbox in grid view created dynamically. We can access the user input as text, but how do I check whether the text is of integer type or string type?

if (tx.Text == "")
{
    tx.Text = Convert.ToString(0);
}
if (Convert.ToInt32(tx.Text) > max)
{
    MessageBox.Show("Some Message", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
    tx.Text = Convert.ToString(max);
}

tx is the textbox from which we are accessing the user input through tx.Text.
How can I check the type of the input parameter whether it is Integer or not?

Upvotes: 1

Views: 9620

Answers (4)

Bani
Bani

Reputation: 1

I think the following approach is more simple. You need just to set a method for checking actions with textBox:

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
    {

        return;
    }

    if (Char.IsControl(e.KeyChar))
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            Button1.Focus ();
        }
    }
  e.Handled = true;

}

Upvotes: 0

callisto
callisto

Reputation: 5083

I use this:

 private dataType ParseString(string str)
        {
            bool boolValue;
            Int32 intValue;
            Int64 bigintValue;
            decimal doubleValue;
            DateTime dateValue;

            // Place checks higher in if-else statement to give higher priority to type.

            if (bool.TryParse(str, out boolValue))
                return "System_Boolean";
            else if (Int32.TryParse(str, out intValue))
                return "System_Int32";
            else if (Int64.TryParse(str, out bigintValue))
                return "System_Int64";
            else if (decimal.TryParse(str, out doubleValue))
                return "System_Decimal";
            else if (DateTime.TryParse(str, out dateValue))
                return "System_DateTime";
            else return "System_String";

        }

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244913

You're looking for the TryParse method. That will tell you if a given string value can be converted into a number. And it does it all without throwing any exceptions.

Sample code:

  int number;
  bool result = Int32.TryParse(tx.Text, out number);
  if (result)
  {
      // Conversion to a number was successful.
      // The number variable contains your value.        
  }
  else
  {
     // Conversion to a number failed.
     // The value entered in the textbox is not numeric.
  }

But if you're looking to restrict the input range of the textbox (i.e., prevent the user from entering anything but numbers), this is not the correct way to go about it.

Instead, you should use a different control, such as a NumericUpDown control, or a MaskedTextBox control. These allow you to prevent the user from entering invalid input in the first place, which is much more user friendly than showing an error after the fact.


In response to your comment:

in java you have instanceof keyword to check for object type. eg:- Obj instanceof Integer ...How do you check for the object type in C#

C# has the typeof keyword, but that's not going to help you here. The problem is, the object you're checking is an instance of type String. The Text property of the TextBox class always returns an object of type String. This is not VB 6: there are no Variants here. What you're checking is whether that String value can be converted into an equivalent integral representation.

Upvotes: 7

Shyju
Shyju

Reputation: 218852

int intNumber;    
bool result=int.TryParse(txt.Text, out intNumber)

If the string (your text box value) contains nonnumeric characters or the numeric value is too large or too small for the particular type you have specified, TryParse returns false and sets the out parameter to zero. Otherwise, it returns true and sets the out parameter to the numeric value of the string.

Here is the msdn link with example : http://msdn.microsoft.com/en-us/library/bb384043.aspx

Upvotes: 0

Related Questions