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.IsNullOrEmptyon 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 yourIsNothingchecks 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 IfHowever 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 IfKevB : If the string variable is empty and you do a split on it, the array will still contain 1 item, therefore items will always not be nothing.Binary Worrier : mdresser: You have actually answered the question as asked in the title "VB.NET missing isNull() ?", but the answer doesn't make sense in terms of the code sample given. If I were you I'd edit the answer to state that fact. Hope this helps.mdresser : @Binary Worrier: thanks for the suggestion. I'll adjust my answer very soon. -
The keyword for null in VB is
Nothing.However, this is not what you want to use in this case. The
Splitmethod 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.
Guffa : No, the returned array always has at least one item.Joel Coehoorn : @Guffa: not true. If you use StringSplitOptions.RemoveEmptyEntries you can get an empty array back. -
you may also want to check for empty strings withOK, 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 ifJoel Coehoorn : Ugh, no. Use String.IsNullOrEmpty()
0 comments:
Post a Comment