Why use vbNullString-VBForums
Results 1 to 32 of 32

Thread: Why use vbNullString

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Why use vbNullString

    Hello, I'm new to this forum so I've been spending some time browsing it and I've noticed that some times you guys use vbNullSting why is that? Is vbNullString the same thing as an empty string? If it is why not just use an empty string instead of typing the rather long word "vbNullString"?
    Share on Google+

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Why use vbNullString

    vbnullstring does empty the string but it also removes it from memory so it is slightly more efficient as far as memory is concerned.

    casey.
    Share on Google+

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    I don't understand. What is it that is removed from memory? If I would use: Label1.Caption = vbNullString instead of using Label1.Caption = "" what is it that is removed? Obviously the old string that was contained in the caption is removed but what is the difference between them?
    Share on Google+

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Why use vbNullString

    To my undestanding a "" string still has some data in it. vbNullString has no data. So, Label1.Caption = vbNullString would have no data, whereas Label1.Caption = "" would have those special bits of data. Meh, at times the more complicated languages are easier to understand.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM
    Share on Google+

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Why use vbNullString

    Has chr(0), end of string
    Share on Google+

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    Quote Originally Posted by dglienna
    Has chr(0), end of string
    Now I'm really confused. If I would use LenB("") 0 is returned so no extra data seems to be there. Same thing with LenB(vbNullString), but LenB(Chr(0)) returns 2 wich is logical since it's a 1 character string in Unicode format.

    If I look in the object browser the definition of vbNullString is: Const vbNullString = "" which to me looks like vbNullString is exactly the same thing as "", so I don't see what the advantage would be to use it.

    It also seems to be a bit confusion among you members on what vbNullString is
    Share on Google+

  7. #7
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,688

    Re: Why use vbNullString

    "" requires 2 bytes of memory and vbNullString requires only 1 byte of memory or some logic like that I cant remember exactly what
    it was but vbNullstring is recommended.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Senior Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007
    Share on Google+

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    Are you saying that vbNullString is not an empty Unicode string? But VB uses Unicode strings so if vbNullString is not in Unicode format wouldn't VB need to first convert it to Unicode before it can use it internally? If that is the case then vbNullString would be slower to use... This subject is very confusing to me... Is there anything like an empty Unicode string in the first place?
    Share on Google+

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,688

    Re: Why use vbNullString

    I found this on MS that is pertaining to passing vbNullString.

    Note that using vbNullString is equivalent to passing NULL as the parameter. This is not the same as passing an empty string ("").
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Senior Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007
    Share on Google+

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    Thanks RobDog888, but that confused me even more. If the Object Browser say that vbNullString is equal to "" but MS say that it's the same as NULL what does that actually mean? And why do people say that it's faster to use vbNullString instead of ""?
    Share on Google+

  11. #11
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,688

    Re: Why use vbNullString

    I dont remember the exact reasoning but it had to do with memory and the fact that its a zero length const. Whereas a ""
    is 2 characters in length and requires more bits of memory. Now remember the quote I posted was in regards to passing a nullstring argument and
    may not be the same for other uses.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Senior Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007
    Share on Google+

  12. #12
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,238

    Re: Why use vbNullString

    This subject is obviously very confusing to most members. I did a quick Google search to see if I could find a better way to explain this then how I'm now going to do it, but I only found just as confusing explanations that is found here. Statements like "when you use "" VB will try to create a null string but it can't so vbNullString is better..." is what I found which doesn't explain anything and in fact are completly wrong.

    For me to explain this you first need to know what a VB string really is. VB use something called a BSTR which is basically a pointer to a pointer (if you know some C). That means that the string contains a memory address to another memory address that points to where the text is actually located. I know this sounds a bit confusing so have a look at this where I explain this a bit further.

    vbNullString was introduced in VB4 and was intended to be used with API calls that required a string. You would use this constant if you wanted to pass NULL to the string argument. The only otherway you could pass NULL to the API function would be to change the argument in the API declaration to accept a Long instead of a String and pass 0 by value.

    So vbNullString is a string constant that points to the memory address 0 (it claims that the text it contains is located at that memory address). You can see this by doing this:
    VB Code:
    1. Debug.Print StrPtr(vbNullString)
    This is not the same as doing this:
    VB Code:
    1. Debug.Print StrPtr("")
    As you can see you will always get 0 when you look at the string pointer of vbNullString while for an empty string that you create you will get different addresses for the empty string you've created.

    So vbNullString is usefull for passing NULL to API calls. When you use it in other locations like in an assignment to a property value or to a string variable VB will simply treat this as an empty string, so it's in fact the same thing as "", since assigning to a variable in VB will copy the string value not the string address. So the fact that vbNullString points to a NULL value (it doesn't contain a NULL character, that's something else) doesn't make it faster. In fact it has nothing todo with which memory location it points to at all.

    So why do people say you should use it instead of using ""? Why is it claimed to be faster to use vbNullString then to use ""? Or isn't it faster at all?

    Yes it is slightly faster to assign vbNullString then to assign "" but it has nothing todo with the fact that it points to 0. The simple reason that vbNullString is faster is because it's a string that has already been created so VB doesn't have to create a new one. Everytime you use "" VB will create a new empty string which takes a little time. It then copies this empty string to the location you assign it to. Using the vbNullString will simply copy this empty string to a new location as well, but the difference is that vbNullString already exists while VB has to create the "" string first.

    You can skip using vbNullString and instead use your own constant and that would be just as fast. Let's say you have the following declaration in a standard BAS module:
    VB Code:
    1. Public Const MyEmptyString As String = ""
    Now using MyEmptyString when you want to assign an empty string to a string property or a string variable will be just as fast as using vbNullString simply because VB will create the empty string only once, when your application starts and you can then reuse the same string over and over.

    I hope this makes this issue a little bit clearer for everyone

    vbNullString is a BSTR that points to 0. It doesn't take up less memory then another empty string. It doesn't contain a NULL char, it's simply a pointer to NULL. But unless you're interested in the address the string is located (which you in VB normally aren't unless you use it as an argument to an API call) it's just a predefined empty string, nothing else.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP
    Share on Google+

  13. #13
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Why use vbNullString

    If you do not know what a BSTR is then this may confuse the issue more.

    Null Strings and Null Characters
    To its credit, VB does allow null BSTRs. The code:

    Dim s As String
    s = vbNullString
    Debug.Print VarPtr(s)
    Debug.Print StrPtr(s)

    produces the following output (your address may vary, of course):

    1243948
    0

    This shows that a null BSTR is simply a pointer whose contents are 0. (We will discuss the meaning of StrPtr in a moment.) In Win32 and VC++, this is called a null pointer. You can probably see the difference between vbNullString and vbNullChar at this point. vbNullChar is not a pointer--it is a Unicode character whose value is 0. Thus, at the bit level, the values vbNullString and vbNullChar are identical. However, they are interpreted differently, so they are in fact different.

    It is also important not to confuse a null BSTR with an empty BSTR, usually denoted by a pair of adjacent quotation marks:

    Dim s As String
    Dim t As String
    s = vbNullString
    t = ""

    Unlike a null string, the empty BSTR t is a pointer that points to some nonzero memory address. At that address resides the terminating null character for the empty BSTR, and the preceeding length field also contains a 0.
    Share on Google+

  14. #14
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Why use vbNullString

    Quote Originally Posted by Joacim Andersson
    For me to explain this you first need to know what a VB string really is. VB use something called a BSTR which is basically a pointer to a pointer (if you know some C). That means that the string contains a memory address to another memory address that points to where the text is actually located.
    I know you know what you're talking about but I got confused on this bit so I thought others might as well. Just to clarify, a BSTR is a 4 byte pointer that points to the start of the string contents. It does not point to another pointer that points to the string contents. The 4 bytes at the address you get from VarPtr(MyString) is the same as what is returned by StrPtr(MyString).
    Share on Google+

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    Thank you so very much Joacim for that in depth explanation. It's easy to see why you are called a guru So using vbNewLine is faster simply because it's a predefined string.

    Now I only have one other question. Is this forum like many others that requires me to have a gezillion posts before I can give you a rating or would it count if I do it now?
    Share on Google+

  16. #16
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,238

    Re: Why use vbNullString

    Quote Originally Posted by penagate
    I know you know what you're talking about but I got confused on this bit so I thought others might as well. Just to clarify, a BSTR is a 4 byte pointer that points to the start of the string contents. It does not point to another pointer that points to the string contents. The 4 bytes at the address you get from VarPtr(MyString) is the same as what is returned by StrPtr(MyString).
    No penagate, I'm afraid you're wrong. The VarPtr of a string contains one address and StrPtr another. Have a look at the image I created here that explains all of this a bit better. Also when I run this code:
    VB Code:
    1. Dim str As String
    2. str = "Hello World!"
    3. Debug.Print "VarPtr = " & VarPtr(str)
    4. Debug.Print "StrPtr = " & StrPtr(str)
    I got the following output:
    VarPtr = 1242176
    StrPtr = 60245812
    You would of course get other addresses but you can clearly see that they are different.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP
    Share on Google+

  17. #17
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Why use vbNullString

    I knew I shouldn't have started, but anyway

    What I was trying to say was, At the address returned by VarPtr, you have a 4 byte pointer to the start of the string. StrPtr returns that value, but VarPtr returns the address at which the pointer is stored.

    If you do this
    VB Code:
    1. Dim lptrString As Long
    2. Dim sTest As String
    3. sTest = "Something"
    4.  
    5. RtlMoveMemory lptrString, ByVal VarPtr(sTest), 4
    6.  
    7. Debug.Print lptrString
    8. Debug.Print StrPtr(sTest)
    the results are the same. That's what I was trying to say
    Share on Google+

  18. #18

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    Please don't confuse this anymore . The image Joacim have in that other thread clearly shows the relationship between the VarPtr and the StrPtr. I come from the C++ world and fully understand what a pointer to a pointer is and I also know how a BSTR is constructed. I now also understand why it's faster in VB to use a predefined string, it's simple when you think about it

    My question now is how many posts I need to make before I can rate an answer and that rating is valid.
    Share on Google+

  19. #19
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Why use vbNullString

    Twenty.
    Share on Google+

  20. #20

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: Why use vbNullString

    Thanks for that.
    Share on Google+

  21. #21
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,247

    Re: Why use vbNullString

    As an addendum . . .

    'vbnullstring' is not actually a BSTR* (pointer to a BSTR) it's a pointer to NULL which sits on the symbol table, and is compiled in line when VB compiles.

    As previously noted the only way to tell between "" and vbnullstring is to use the hidden StrPtr function.

    I only note this because you can get into trouble passing a true NULL to some API functions (vbnullstring), when you should really be passing a BSTR that contains nothing ("")
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down
    Share on Google+

  22. #22
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,072

    Re: Why use vbNullString

    Good thing this debate ended a couple weeks ago. It wasn't confusing to me at all. I got the point 10 posts ago. Might confuse noobs though.
    Share on Google+

  23. #23
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,247

    Re: Why use vbNullString

    TBH don't particularly care about ratings.

    If someone is searching the forums for API problems, then my post, I think, is the right place to be picked up should the issue I mention is applicable.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down
    Share on Google+

  24. #24
    Hyperactive Member
    Join Date
    Oct 2008
    Posts
    411

    Re: Why use vbNullString

    So in MS's documentation about the PlaySound API function, where it talks about the pszSound parameter (which is declared byval as string in the VB6 API documentation), where it says
    pszSound

    A string that specifies the sound to play. The maximum length, including the null terminator, is 256 characters. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.
    Would the NULL it's talking about in this documentation be referring to the null character (ascii code 0, vbNullChar), or an empty string "", or the special string constant vbNullString, or the special VB constant actually called NULL?
    Share on Google+

  25. #25
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Why use vbNullString

    to be clear look

    text1.text= ""
    or
    text1.text =vbnullstring


    dim somedata as string
    somedata = vbnullstring ' this is what vbnullstring is used for , for data like these

    i cant use
    somedata ="" <<<<<<<<<<< this will give error

    so this is why for data such as
    somedata = vbnullstring is required here only not "" or else face error hope its clear

    with any textbox u can use "" but data strings u need to use vbnullstring
    what i mean by data string is

    dim lll as string
    dim love as string
    dim ddos as string , fartspray as string

    ddos=vbnullstring
    fartspray = vbnullstring
    Last edited by ladoo; May 19th, 2013 at 07:26 AM.
    Share on Google+

  26. #26
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,238

    Re: Why use vbNullString

    You guys do realize that this is an 8 year old thread, right?

    NULL for strings in API's = vbNullString not vbNullChar since that would be equal to an empty string in C.

    Quote Originally Posted by ladoo View Post
    i cant use
    somedata ="" <<<<<<<<<<< this will give error
    Sorry but that is just completely wrong. What gave you that idea? Of course you can assign an empty string to a string.
    Code:
    Dim someValue As String
    someValue = ""
    The assignment must of course be inside a Sub or a Function but that code is absolutely valid.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP
    Share on Google+

  27. #27
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Why use vbNullString

    Quote Originally Posted by Joacim Andersson View Post
    You guys do realize that this is an 8 year old thread, right?

    NULL for strings in API's = vbNullString not vbNullChar since that would be equal to an empty string in C.

    Sorry but that is just completely wrong. What gave you that idea? Of course you can assign an empty string to a string.
    Code:
    Dim someValue As String
    someValue = ""
    The assignment must of course be inside a Sub or a Function but that code is absolutely valid.



    proof me wrong, open a project try it
    Share on Google+

  28. #28
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,238

    Re: Why use vbNullString

    Oh boy! Do you know how many projects I've created in VB6 through the years? This works perfectly:
    Code:
    Private Sub Command1_Click()
        Dim value As String
        value = ""
        MsgBox value
    End Sub
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP
    Share on Google+

  29. #29
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: Why use vbNullString

    wat theeeeeee i tried that last time many times gave error,,,,,,,,,,,,,,,,,,,,,,,,, ok so its clear now am wrong
    Share on Google+

  30. #30
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,238

    Re: Why use vbNullString

    The only time that would result in an error is if you try to put that code outside of a sub or function. You can declare a variable at that point but not assign any value to it.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP
    Share on Google+

  31. #31
    Addicted Member
    Join Date
    Jan 2013
    Posts
    223

    Re: Why use vbNullString

    I don't thinks that when in the code you do

    a$ = "", VB will actualy create a BSTR struct plus allocate the string in memory to point to it

    that is wrong.

    First, as C,

    NULL = 0 as address ¿why you can pass ZERO as an address?. Is because all API in its string handling libraries or just when handling the parameters, as first place does a test, like:

    if (addr==0)
    { // acts or flags that there is not an input // }
    else
    { // extract string content, from the memory starting on the given address}

    so, no API will take the address zero in ram looking for a string, that is a convention!!.

    now.


    when you set up string CONSTANTs in yout VB code like


    A$ = ""
    A$ = "abc"

    the string is already placed in the code segment, it exists!, VB don't needs to allocate and or create a copy of it to work with it (about reading the content of the string constant and copying it to A$)!.

    But there is one trust.

    giving to an API a "" is not giving a NULL.

    because from the VB perspective, ("","",""....) will give to the API the adress of that hard-coded stringconstant !!!in the code segment!!!. Again, I don't thinks that VB creates a BSTR instance just for pick up something hardcoded in the code segment). So, no speed gain about assigning A$ = "" vs A$ = vbnullstring

    And if the API needs something like a null char terminated, the problem is that passing ("") will not give to it, placing in the code segment a null char. As a way to do the same effect, in APIs expecting strings null terminated and not like BUFFERs where the length is passed as another parameter.
    Last edited by flyguille; May 19th, 2013 at 03:55 PM.
    Share on Google+

  32. #32
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,238

    Re: Why use vbNullString

    I will close this thread since it was 8 years old but I just want to clarify what I wrote 8 years ago.

    VB always creates the BSTR when you declare a string, even if the actual text already exists on the heap in which case the BSTR will point to that location but the actual BSTR still needs to be created (the two pointers).

    When you pass a string to an API function the BSTR is converted to a C style null terminated string (so it's not a BSTR any more).

    Nobody in this thread ever claimed that "" was the same as NULL.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP
    Share on Google+

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.