VB6 等效于 string.IsNullOrEmpty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8762345/
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
VB6 equivalent of string.IsNullOrEmpty
提问by Justin Morgan
I'm doing some work on a legacy application, and my VB6 skills aren't that great. I need to check whether a String field has been initialized and set to something other than null/nothing or an empty string. In C# I'd just do something like:
我正在做一些遗留应用程序的工作,而我的 VB6 技能并不是那么好。我需要检查 String 字段是否已初始化并设置为 null/nothing 或空字符串以外的其他内容。在 C# 中,我只会做类似的事情:
if (string.IsNullOrEmpty(myObj.Str))
I'm not sure what the equivalent to this was in VB6, and I'm nervous about using If myObj.Str = ""
and calling it good. What's the correct way to do this?
我不确定 VB6 中的等价物是什么,我对使用If myObj.Str = ""
它并称其为好感到紧张。这样做的正确方法是什么?
To clarify, I want something that will return True if any of the following are true:
为了澄清,如果以下任何一项为真,我想要一些将返回 True 的东西:
- The field hasn't been initialized
- The field is an empty string (str = "", length = 0)
- The field is set to null, or Nothing, or vbnull, or whatever form of the null value applies to VB6 strings.
- 该字段尚未初始化
- 该字段为空字符串(str = "", length = 0)
- 该字段设置为 null、Nothing 或 vbnull,或适用于 VB6 字符串的任何形式的 null 值。
The field was originally a Long, and the code I'm replacing checked whether it was set to 0.
该字段最初是一个 Long,我正在替换的代码检查它是否设置为 0。
回答by MarkJ
VB6 was designed to be easy
VB6 的设计很简单
Use
用
If str = "" Then
' uninitialised, null or empty ""
- Strings are automatically initialized to [edit] a null string.
- The null string is vbNullString.
- But don't worryabout null strings. A VB6 null string is indistinguishable from an empty string "" for (almost) any string manipulation.
- 字符串会自动初始化为 [编辑] 空字符串。
- 空字符串是 vbNullString。
- 但是不要担心空字符串。对于(几乎)任何字符串操作,VB6 空字符串都与空字符串 "" 没有区别。
回答by Warren Rox
The most optimized and safe way to meet your 3 requirements is as follows:
满足您的 3 个要求的最优化和最安全的方法如下:
If LenB(myObj.Str) = 0
Then Debug.Print "String is empty/null/not initialized"
Else Debug.Print "Not Empty"
回答by Thomas
use Is Nullfor stings or is nothingfor objects
将Is Null用于 stings 或is nothing用于对象
use len("string") instead of "string"="" because it is faster
使用len(" string") 而不是 " string"="" 因为它更快
Dim s As String
If Not (s Is Null) Then
MsgBox "SET"
if (len(s)>0) then
MsgBox "size > 0"
else
MsgBox "size = 0"
end if
Else
MsgBox "not SET"
End If
regards
问候
回答by Virendra Vaishnav
I tried, If str = ""
Then
我试过了,If str = ""
然后
but it did not work.
但它没有用。
The best way to identify if the given string is null is:
识别给定字符串是否为空的最佳方法是:
If IsNull(str)
Then
If IsNull(str)
然后
' This will work perfectly
'这将完美地工作
回答by Gonzalo
Just adding info to MarkJ's answer. If you are working with a recordset, this
只是在 MarkJ 的答案中添加信息。如果您正在使用记录集,这
if rs.fields.item("rsField").value = "" then
Will throw a runtime error in case that item is null. You should do this:
如果该项目为空,将引发运行时错误。你应该做这个:
if rs.fields.item("rsField").value & "" = "" then
Good luck.
祝你好运。
回答by KoyKoy
'Using VbNullstring can be a very effective way
'使用 VbNullstring 是一种非常有效的方法
Dim YourString as string
dim HasValue as boolean
If YourString = vbnullstring then
HasValue = False
Else
HasValue = True
End if
回答by Matt Donnan
As 'Nulls' and 'Empty Strings' are actually different values, this approach can capture both for you easily:
由于 'Nulls' 和 'Empty Strings' 实际上是不同的值,这种方法可以轻松地为您捕获两者:
If strValue & "" = "" Then
'This is Null
Else
'This is NOT Null
End If