2

May I know got any simple way to perform this? It will hit an error when a.text is null. If I don't detect one by one, With a simple code may I convert a.text null to 0?

Dim count1 As Integer = 0
count1 = Convert.ToInt32(a.Text) + Convert.ToInt32(b.Text) + Convert.ToInt32(c.Text)
txt_display.Text = count1

Got any other method rather that I do like below detect one by one.

if a.Text = "" Then
a.Text = 0
End If
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Mike
  • 177
  • 1
  • 2
  • 13

5 Answers5

4

You have to detect one by one. Better way, will be to create your own function. Try below.

Dim count1 As Integer = 0
count1 = ConvertToInteger(a.Text) + ConvertToInteger(b.Text) + ConvertToInteger(c.Text)
txt_display.Text = count1




Private Function ConvertToInteger(ByRef value As String) As Integer
    If String.IsNullOrEmpty(value) Then
        value = "0"
    End If
    Return Convert.ToInt32(value)
End Function
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
3

If your objective is to sum the values in your textboxes and ignore the textboxes that cannot be converted to integers then you can simply use Int32.TryParse.
It will set your variable to 0 if the text cannot be converted to an integer without throwing exceptions.

' In place of your textboxes
Dim x1 As String = "2"
Dim x2 As String = Nothing
Dim x3 As String = "5"

Dim a, b, c As Integer

Int32.TryParse(x1, a)
Int32.TryParse(x2, b)
Int32.TryParse(x3, c)

Dim result = a + b + c
Console.WriteLine(result)

Instead, if you want to write the "0" string into the textbox text to signal your user of the wrong input, then you have to check the textboxes one by one, again using Int32.TryParse

Dim value1 as Integer
if Not Int32.TryParse(a.Text, value1) Then
   a.Text = "0"
End If

' Here the variable value1 contains the converted value or zero.
' repeat for the other textboxes involved
Steve
  • 213,761
  • 22
  • 232
  • 286
3

A different approach using If Operator:

Dim count1 As Integer = 0
count1 = If(String.IsNullOrEmpty(a.Text), 0, Convert.ToInt32(a.Text)) + If(String.IsNullOrEmpty(b.Text), 0, Convert.ToInt32(b.Text)) + If(String.IsNullOrEmpty(c.Text), 0, Convert.ToInt32(c.Text))
txt_display.Text = count1
FN90
  • 421
  • 4
  • 13
1

Example:

If String.IsNullOrEmpty(a.Text) Then
 a.Text = "0"
End If
Svekke
  • 1,470
  • 1
  • 12
  • 20
0

As per the Microsoft Documentation, a null string (Nothing) is different from an empty string (""):

The default value of String is Nothing (a null reference). Note that this is not the same as the empty string (value "").

You also use the = operator, which can be tricky with String types. For more info, see this post and the answer which was awarded a 50 points bounty.

If you use If with only two arguments, the following code

If a.Text Is Nothing Then
    a.Text = 0
End If

Can be turned into a one-liner: Dim MyNumber as Integer = If(a.Text, 0)

If you meant to work with empty strings, then you can use: Dim MyNumber as Integer = If(a.Text.Length = 0, 0, a.Text).

If you want to deal with both, you can use String.IsNullOrEmpty(a.Text) as suggested by the currently accepted answer; or you can use a.Text="", a.Text = String.Empty, or a.Text = vbNullString, which are all equal (see the post I referred to earlier)

Finally, note that the conversion from a String type to an Integer type is made implicitly. There is no need to use the explicit cast conversions such as CType() or Convert.ToInt32.

Ama
  • 1,373
  • 10
  • 24