-
Jul 25th, 2005, 12:28 AM
#1
Thread Starter
Member
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"?
-
Jul 25th, 2005, 12:32 AM
#2
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.
-
Jul 25th, 2005, 12:35 AM
#3
Thread Starter
Member
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?
-
Jul 25th, 2005, 12:39 AM
#4
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
-
Jul 25th, 2005, 12:40 AM
#5
Re: Why use vbNullString
Has chr(0), end of string
-
Jul 25th, 2005, 12:49 AM
#6
Thread Starter
Member
Re: Why use vbNullString
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
-
Jul 25th, 2005, 12:54 AM
#7
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 (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 25th, 2005, 12:58 AM
#8
Thread Starter
Member
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?
-
Jul 25th, 2005, 01:03 AM
#9
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 (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 25th, 2005, 01:13 AM
#10
Thread Starter
Member
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 ""?
-
Jul 25th, 2005, 01:16 AM
#11
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 (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jul 25th, 2005, 01:50 AM
#12
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:
Debug.Print StrPtr(vbNullString)
This is not the same as doing this: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:
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.
-
Jul 25th, 2005, 01:55 AM
#13
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.
-
Jul 25th, 2005, 03:02 AM
#14
Re: Why use vbNullString
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).
-
Jul 25th, 2005, 03:05 AM
#15
Thread Starter
Member
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?
-
Jul 25th, 2005, 03:14 AM
#16
Re: Why use vbNullString
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:
Dim str As String
str = "Hello World!"
Debug.Print "VarPtr = " & VarPtr(str)
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.
-
Jul 25th, 2005, 03:22 AM
#17
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:
Dim lptrString As Long
Dim sTest As String
sTest = "Something"
RtlMoveMemory lptrString, ByVal VarPtr(sTest), 4
Debug.Print lptrString
Debug.Print StrPtr(sTest)
the results are the same. That's what I was trying to say
-
Jul 25th, 2005, 03:35 AM
#18
Thread Starter
Member
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.
-
Jul 25th, 2005, 03:37 AM
#19
-
Jul 25th, 2005, 03:47 AM
#20
Thread Starter
Member
-
Aug 12th, 2005, 09:00 AM
#21
Frenzied Member
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
-
Aug 12th, 2005, 09:11 AM
#22
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.
-
Aug 12th, 2005, 09:13 AM
#23
Frenzied Member
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
-
May 19th, 2013, 07:09 AM
#24
Frenzied Member
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?
-
May 19th, 2013, 07:15 AM
#25
Banned
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.
-
May 19th, 2013, 08:21 AM
#26
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.
Originally Posted by ladoo
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.
-
May 19th, 2013, 01:53 PM
#27
Banned
Re: Why use vbNullString
Originally Posted by Joacim Andersson
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
-
May 19th, 2013, 02:16 PM
#28
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
-
May 19th, 2013, 02:20 PM
#29
Banned
Re: Why use vbNullString
wat theeeeeee i tried that last time many times gave error,,,,,,,,,,,,,,,,,,,,,,,,, ok so its clear now am wrong
-
May 19th, 2013, 02:46 PM
#30
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.
-
May 19th, 2013, 03:50 PM
#31
Fanatic Member
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.
-
May 19th, 2013, 04:23 PM
#32
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|