I like to confirm that array is created, how can it be done? There is no nul keyword?
Dim items As Array = str.Split("|")
if (items=null) then ???
I like to confirm that array is created, how can it be done? There is no nul keyword?
Dim items As Array = str.Split("|")
if (items=null) then ???
Try using String.IsNullOrEmpty
on your string variable before splitting it. If you attempt to split the variable with nothing in the string the array will still have one item in it (an empty string), therefore your IsNothing
checks on the array will return false.
Use "Is Nothing" to test for Null in VB.NET.
If items Is Nothing Then
End If
To check if an object is null in VB.Net you need to use the Nothing keyword. e.g.
If (items is Nothing) Then
'do stuff
End If
However string.Split() never returns null, so you should check the input string for null rather than the items array. Your code could be changed to something like:
If Not String.IsNullOrEmpty(str) Then
Dim items As Array = str.Split("|")
'do stuff
End If
The keyword for null in VB is Nothing
.
However, this is not what you want to use in this case. The Split
method never returns a null reference. It always returns a string array that has at least one item. If the string that you split was empty, you get an array containing one string with the length zero.
So, to check if you get that as result you would do:
Dim items As String() = str.Split("|")
If items.Length = 1 and items(0).Length = 0 Then ...
It's of course easier to check the input first:
If str.Length = 0 Then ...
String.Split can never return null. At worst, it can return an array with no elements.
you may also want to check for empty strings with
OK, OK, I get it, bad advice.
but tell me this:
Do I need to call toString on the object like:
String.IsNullOrEmpty(myTextBox.text.ToString)
or just use:
String.IsNullOrEmpty(myTextBox.text)
if not myString.tostring.trim.equals(string.empty)
'...
end if