vba vbNullString 和 "" 之间有什么区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32435320/
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
Is there any difference between vbNullString and ""?
提问by eirikdaude
In VBA, will it make any difference if I compare a string, or similar, against vbNullStringinstead of against an empty string; ""? If so what differences are there between the two?
在 VBA 中,如果我将字符串或类似字符串与vbNullString空字符串进行比较而不是与空字符串进行比较,会有什么不同吗?""? 如果是这样,两者之间有什么区别?
回答by GoeschWho
vbNullStringand ""are different.Here is a webpage exerpt that describes the memory usage differences.
vbNullString并且""是不同的。这是描述内存使用差异的网页摘录。
"This is the usual way to clear a string variable.
“这是清除字符串变量的常用方法。
Text$ = ""
What a waste! First of all, the string ""takes 6 bytes of RAM each time you use it. Consider the alternative:
多么浪费!首先,""每次使用该字符串时都会占用 6 个字节的 RAM。考虑替代方案:
Text$ = vbNullString
So what is this? vbNullStringis a special VB constant that denotes a null string. The ""literal is an empty string. There's an important difference. An empty string is a real string. A null string is not. It is just a zero. If you know the C language, vbNullStringis the equivalent of NULL.
那么这是什么?vbNullString是一个特殊的 VB 常量,表示一个空字符串。该""文本是一个空字符串。有一个重要的区别。空字符串是真正的字符串。空字符串不是。它只是一个零。如果你懂C语言,vbNullString就相当于NULL。
For most purposes, vbNullStringis equivalent to ""in VB. The only practical difference is that vbNullStringis faster to assign and process and it takes less memory.
对于大多数用途,vbNullString相当于""在 VB 中。唯一的实际区别是vbNullString分配和处理速度更快,占用的内存更少。
If you call some non-VB API or component, test the calls with vbNullStringbefore distributing your application. The function you're calling might not check for a NULL string, in which case it might crash. Non-VB functions should check for NULL before processing a string parameter. With bad luck, the particular function you're calling does not do that. In this case, use "". Usually APIs do support vbNullStringand they can even perform better with it!"
如果您调用一些非 VB API 或组件,请vbNullString在分发应用程序之前测试这些调用。您正在调用的函数可能不会检查 NULL 字符串,在这种情况下它可能会崩溃。非 VB 函数应在处理字符串参数之前检查 NULL。运气不好,您调用的特定函数不会这样做。在这种情况下,请使用"". 通常 API 确实支持vbNullString,他们甚至可以用它表现得更好!”
The rest of the article has additional information on optimizing strings that may be insightful as well.
文章的其余部分提供了有关优化字符串的其他信息,这些信息也可能很有见地。
Full webpage: http://www.aivosto.com/vbtips/stringopt.html
回答by luke_t
This comparisonstates that assigning ""to a variable uses 6 bytes of memory whereas using vbNullStringwill not use any memory.
此比较表明,分配""给变量使用 6 个字节的内存,而 usingvbNullString不会使用任何内存。
Personally, I prefer to evaluate the length of a string. If length is 0, then we also arrive at the conclusion that the string is a vbNullStringor "". This method is accepted as being the quickest method of checking for a vbNullString.
就个人而言,我更喜欢评估字符串的长度。如果长度为 0,那么我们也得出字符串是 a vbNullStringor的结论""。此方法被公认为是检查vbNullString.
If Len(string) = 0 Then
You can read a Lenvs vbNullStringvs ""comparison here.
您可以在此处阅读Lenvs vbNullStringvs""比较。

