问题描述
在 VBA 中,如果我将字符串或类似字符串与 vbNullString
进行比较而不是与空字符串进行比较,会产生什么不同吗?""
?如果是这样,两者之间有什么区别?
In VBA, will it make any difference if I compare a string, or similar, against vbNullString
instead of against an empty string; ""
? If so what differences are there between the two?
推荐答案
vbNullString
和 ""
是不同的. 这是网页摘录描述了内存使用差异.
vbNullString
and ""
are different. Here is a webpage exerpt that describes the memory usage differences.
"这是清除字符串变量的常用方法.
"This is the usual way to clear a string variable.
Text$ = ""
太浪费了!首先,字符串 ""
每次使用时需要 6 个字节的 RAM.考虑替代方案:
What a waste! First of all, the string ""
takes 6 bytes of RAM each time you use it. Consider the alternative:
Text$ = vbNullString
这是什么?vbNullString
是一个特殊的 VB 常量,表示一个空字符串.""
文字是一个空字符串.有一个重要的区别.空字符串是真正的字符串.空字符串不是.它只是一个零.如果你懂 C 语言,vbNullString
就相当于 NULL.
So what is this? vbNullString
is 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, vbNullString
is the equivalent of NULL.
在大多数情况下,vbNullString
等价于 VB 中的 ""
.唯一的实际区别是 vbNullString
分配和处理速度更快,占用的内存更少.
For most purposes, vbNullString
is equivalent to ""
in VB. The only practical difference is that vbNullString
is faster to assign and process and it takes less memory.
如果您调用某些非 VB API 或组件,请在分发应用程序之前使用 vbNullString
测试调用.您正在调用的函数可能不会检查 NULL 字符串,在这种情况下它可能会崩溃.非 VB 函数应在处理字符串参数之前检查 NULL.运气不好,您调用的特定函数不会这样做.在这种情况下,请使用 ""
.通常 API 确实支持 vbNullString
,并且使用它甚至可以表现得更好!"
If you call some non-VB API or component, test the calls with vbNullString
before 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 vbNullString
and they can even perform better with it!"
本文的其余部分提供了有关优化字符串的其他信息,这些信息也可能很有见地.
The rest of the article has additional information on optimizing strings that may be insightful as well.
完整网页:http://www.aivosto.com/vbtips/stringopt.html一个>
这篇关于vbNullString 和“"之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!