vb.net 如何检查字符串数组是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26179038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a string array is null?
提问by Maxim Petrov
I modified a workflow in TFS template, in the head of this workflow I initialized an array of string named NextChainBuildDefinition. After a few steps, I tried to check if this array is null or not.
我修改了 TFS 模板中的一个工作流,在这个工作流的头部,我初始化了一个名为NextChainBuildDefinition. 几步之后,我尝试检查这个数组是否为空。
I did this way:
我是这样做的:
String.IsNullOrEmpty(CStr(NextChainBuildDefinition.Count))
After this I see error: Exception Message: Value cannot be null.Therefore NextChainBuildDefinitionis null, and in that step it throws an exception.
在此之后,我看到错误:Exception Message: Value cannot be null.因此NextChainBuildDefinition为空,并在该步骤中引发异常。
How do I check if this string array is null?
如何检查此字符串数组是否为空?
回答by James Thorpe
You need to check if the array itself is null or empty - your current code is checking if the string conversion of the number of elements in the array is empty - this isn't going to work at all.
您需要检查数组本身是 null 还是空 - 您当前的代码正在检查数组中元素数量的字符串转换是否为空 - 这根本不起作用。
Instead, you need to do a two step check - both for if the array itself is null, and if not, if it's empty:
相反,您需要进行两步检查——检查数组本身是否为空,如果不是,则检查是否为空:
If (NextChainBuildDefinition IsNot Nothing AndAlso NextChainBuildDefinition.Count > 0) Then
'Array has contents
Else
'Array is null or empty
End if
回答by Isaac
Why not just test ubound() of the array? old question, i know.
为什么不只测试数组的 ubound() 呢?老问题,我知道。

