Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to integer and get null equal to 0

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
like image 296
Mike Avatar asked Jun 24 '26 14:06

Mike


1 Answers

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
like image 92
Mukul Varshney Avatar answered Jun 26 '26 19:06

Mukul Varshney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!