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
.