Subscribe to The Madman Scribblings        RSS Feed
***** 2 Votes

Textboxes and Numbers (Are these Numbers?)

Icon 1 Comments
What does the two textboxes contain?
Posted Image

a. they contain the numbers 123 and 456 respectively.
b. they contain strings that can be interpreted as the numbers 123 and 456.

If you answered a then you are in precise in your thinking.
To you, you see numbers, your not thinking about how the computer interpretation of them.

If you answered b then you are correct because the type of the textbox.text is a string.

Remember this fact will help you minimize casting errors like Unable to cast String to Double

It is a string not a number and such can contain non-numerical characters. so check they are valid numbers before treating them as such.

  Dim ValueOfA As Double
  If Double.TryParse(Me.Txt_NumberA.Text, ValueOfA) = False Then
   MessageBox.Show("Number A is not a valid number")
   Exit Sub
  End If
  Dim ValueOfB As Double
  If Double.TryParse(Me.Txt_NumberA.Text, ValueOfB) = False Then
   MessageBox.Show("Number B is not a valid number")
   Exit Sub
  End If
  Dim result As Double = ValueOfA * ValueOfB
  MessageBox.Show(String.Format("Result of {0} * {1} = {2}", ValueOfA, ValueOfB, result))

1 Comments On This Entry

Page 1 of 1

Michael26 Icon

20 March 2013 - 03:49 AM
I don't understand why people can't use the numericUpDown control and avoid casting altogether, it's totally unnecessary
Page 1 of 1