Results 1 to 10 of 10

Thread: [RESOLVED] Null strings

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Null strings

    I have read somewhere in the forums that this statement:

    If myStr = "" Then

    is less convenient (efficient?) than:

    If myStr = vbNull Then

    But I can't remember if the correct constant above is vbNull, vbNullChar, vbNullStr or what...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Null strings

    I prefer

    vb Code:
    1. If Len(Trim(myStr)) = 0 then
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Null strings

    Quote Originally Posted by koolsid View Post
    I prefer

    vb Code:
    1. If Len(Trim(myStr)) = 0 then
    Any special reason?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Null strings

    The constant you want is vbNullString

    It uses slightly less memory, and is slightly faster. Apparently the fastest way is to use LenB, eg:
    Code:
    If LenB(myStr) = 0 Then

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Null strings

    vb Code:
    1. Sub Sample()
    2.     Dim Strg As String
    3.    
    4.     Strg1 = ""
    5.     Strg2 = " "
    6.    
    7.     '~~> If we look at it both strings are empty though the 2nd one has an empty space
    8.     If Strg1 = vbNullString Then MsgBox "Sting 1 is Empty"
    9.    
    10.     If Len(Trim(Strg2)) = 0 Then MsgBox "Sting 2 is Empty"
    11. End Sub

    Edit: Sigh! Speedy Si at work
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Null strings

    Quote Originally Posted by si_the_geek View Post
    The constant you want is vbNullString

    It uses slightly less memory, and is slightly faster. Apparently the fastest way is to use LenB, eg:
    Code:
    If LenB(myStr) = 0 Then
    Thanks, I had tried with vbnullstr and was surprised that it was not acknowledged as a vb constant.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Null strings

    Quote Originally Posted by koolsid View Post
    vb Code:
    1. Sub Sample()
    2.     Dim Strg As String
    3.    
    4.     Strg1 = ""
    5.     Strg2 = " "
    6.    
    7.     '~~> If we look at it both strings are empty though the 2nd one has an empty space
    8.     If Strg1 = vbNullString Then MsgBox "Sting 1 is Empty"
    9.    
    10.     If Len(Trim(Strg2)) = 0 Then MsgBox "Sting 2 is Empty"
    11. End Sub

    Edit: Sigh! Speedy Si at work
    I see what you mean, but " " may or may not be considered an empty string, depending on the specific situation.

    I started the thread because I wanted to prevent errors when reading a text file line-wise. If there was a vbcrlf at the end of the last non-empty line then a null string was read next.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Null strings

    Thanks, I had tried with vbnullstr and was surprised that it was not acknowledged as a vb constant.
    There's no need to remember (or type) the whole thing, because VB will happily tell you what they all are.. just type vbNu and press Ctrl-Space (and if you type vbNulls it will complete it for you).

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Null strings

    Quote Originally Posted by si_the_geek View Post
    There's no need to remember (or type) the whole thing, because VB will happily tell you what they all are.. just type vbNu and press Ctrl-Space (and if you type vbNulls it will complete it for you).
    I always forget about ctrl/space. At any rate, I assume vbnull is for objects, but what would the difference be between vbnullchar and vbnullstring?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Null strings

    vbNull is actually a number, for comparing with the result of (I think) the VarType function.

    vbNullChar is the same as Chr$(0), but quicker because it is a constant rather than a function.

    vbNullString is a string with a null pointer (which is needed for some API's), but within VB it is the equivalent of an empty string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width