VB6 equivalent of string.IsNullOrEmpty

后端 未结 7 1357
野趣味
野趣味 2020-12-29 20:40

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 th

相关标签:
7条回答
  • 2020-12-29 21:00

    use Is Null for stings or is nothing for objects

    use len("string") instead of "string"="" because it is faster

    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

    0 讨论(0)
  • 2020-12-29 21:05

    VB6 was designed to be easy

    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 worry about null strings. A VB6 null string is indistinguishable from an empty string "" for (almost) any string manipulation.
    0 讨论(0)
  • 2020-12-29 21:08

    I tried, If str = "" Then

    but it did not work.

    The best way to identify if the given string is null is:

    If IsNull(str) Then

    ' This will work perfectly

    0 讨论(0)
  • 2020-12-29 21:14

    'Using VbNullstring can be a very effective way

    Dim YourString as string  
    dim HasValue as boolean
    
    If YourString = vbnullstring then  
        HasValue = False  
    Else  
    HasValue = True  
    End if
    
    0 讨论(0)
  • 2020-12-29 21:16

    Just adding info to MarkJ's answer. If you are working with a recordset, this

    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.

    0 讨论(0)
  • 2020-12-29 21:22

    The most optimized and safe way to meet your 3 requirements is as follows:

    If LenB(myObj.Str) = 0 
    Then Debug.Print "String is empty/null/not initialized" 
    Else Debug.Print "Not Empty"
    
    0 讨论(0)
提交回复
热议问题