|
-
Jul 10th, 2006, 06:18 AM
#1
[RESOLVED] String Manipulation. "" vs chr(0)
Hello
RegSetValueEx returns 87 in Windows98se when i attempt to save the data "" to a registy value using RegSetValueEx. But win2k and XP accepes "" data.
So i had to replace "" with chr(0) in windows98se.
What is the real big diffrent between "" and chr(0) ?
is my conversion movement correct?
Why 98se refuse to accept ""
Is same case in 95 and me?
If my move correct i have a big work ahead. i have to replace all the places
where a "" value could returne with the char(0)
-
Jul 10th, 2006, 06:28 AM
#2
Addicted Member
Re: String Manipulation. "" vs chr(0)
"" is an empty string
It's better programming practice to use vbNullString in place of ""
-
Jul 10th, 2006, 06:36 AM
#3
Re: String Manipulation. "" vs chr(0)
Thank you for the posts.
So if a "" returns then we have to conver it as
VB Code:
if strval="" then strval=chr(0)
Is this the way?
-
Jul 10th, 2006, 06:47 AM
#4
Member
Re: String Manipulation. "" vs chr(0)
look my dear
this " " mean the value equial nothing but chr(0) mean that value contain 0 byte
For example :
if you use chr(0) to any variables so you can't let any value > 0 any you cant Dim any data > 0 okk man because you specialize this variables but in second edition
if you use " " you can specialize any data with any size for it if you want to use it again ........
end :
if you want to make variable const mean cant change the size of it so use chr(0) or you want
if you want to contriol to the size of variable ok use " "
byyyyyyyye man and anything else here you are
-
Jul 10th, 2006, 06:54 AM
#5
Re: String Manipulation. "" vs chr(0)
Hai max and david for the support.
i found this too now : "vbNullString = chr(0) # String having value 0 Not the same as a zero-length string ("")
Let me rate your posts and close the thread.
-
Jul 10th, 2006, 07:23 AM
#6
Re: String Manipulation. "" vs chr(0)
vbNullstring does not equal Chr$(0), they are different things.
Chr$(0) (or vbNullChar) - is a null character
"" - a string with zero length
vbNullString - a string with no memory assigned
The StrPtr function returns the address that a string has in memory, and can be used to highlight the difference:
VB Code:
Private Sub Form_Load()
Dim s1 As String, s2 As String, s3 As String
s1 = Chr$(0)
s2 = ""
s3 = vbNullString
Debug.Print Len(s1), Len(s2), Len(s3)
Debug.Print StrPtr(s1), StrPtr(s2), StrPtr(s3)
End Sub
both the first two exist in memory, whereas the third uses no memory.
-
Jul 10th, 2006, 07:29 AM
#7
Re: String Manipulation. "" vs chr(0)
Hai bush. that was a great example.
i picket vbnullstring=chr(0) in this url : http://vb2py.sourceforge.net/docs/vb...t-of-constants
-
Jul 10th, 2006, 08:03 AM
#8
Re: String Manipulation. "" vs chr(0)
So text boxes with no text always returns "" .
so for legacy windows compatibality we need to convert "" to chr(0) as i sad in #4?
as i remeber in my testing win98 also not accept vbnullstring to save to registy. as i remember. it requir a chr(0) to save a "" returned from a textbox with no text.
-
Jul 10th, 2006, 08:09 AM
#9
Re: String Manipulation. "" vs chr(0)
 Originally Posted by Fazi
So text boxes with no text always returns ""
Well, if we test it:
VB Code:
Dim s As String
s = Text1.Text
Debug.Print StrPtr(s)
we get 0, so Text1.Text returns vbNullString if empty, not ""
 Originally Posted by Fazi
so for legacy windows compatibality we need to convert "" to chr(0) as i sad in #4?
as i remeber in my testing win98 also not accept vbnullstring to save to registy. as i remember. it requir a chr(0) to save a "" returned from a textbox with no text.
I don't know what the situation is regarding saving to the registry on 98, i was making the point that Chr$(0), "" and vbNullString are all different things.
-
Jul 10th, 2006, 08:43 AM
#10
Re: String Manipulation. "" vs chr(0)
 Originally Posted by bushmobile
Well, if we test it:
VB Code:
Dim s As String
s = Text1.Text
Debug.Print StrPtr(s)
we get 0, so Text1.Text returns vbNullString if empty, not ""
Bush.. Thank you for the point.
-
Jul 10th, 2006, 09:05 AM
#11
Re: [RESOLVED] String Manipulation. "" vs chr(0)
If you have made a single function for writing and reading to the registry (which is what nearly everyone does!!) you can add checks in there to deal with this a different way. when you try to write a value that is blank "" replace it with "$EMPTY$" or something and then when you read it back check if its "$EMPTY$" and return a "" instead. It may sound like a potty idea, but theres more than one way to skin a cat...
-
Jul 10th, 2006, 11:02 AM
#12
Re: [RESOLVED] String Manipulation. "" vs chr(0)
 Originally Posted by Grimfort
If you have made a single function for writing and reading to the registry (which is what nearly everyone does!!) you can add checks in there to deal with this a different way. when you try to write a value that is blank "" replace it with "$EMPTY$" or something and then when you read it back check if its "$EMPTY$" and return a "" instead. It may sound like a potty idea, but theres more than one way to skin a cat...
Hai Grimfort
It sounds like a good idea too.
Thank you.
-
Jul 10th, 2006, 11:14 AM
#13
Re: String Manipulation. "" vs chr(0)
 Originally Posted by Fazi
as i remeber in my testing win98 also not accept vbnullstring to save to registy. as i remember. it requir a chr(0) to save a "" returned from a textbox with no text.
There is an explanation for this (as there is for most things that don't involve too great a degree of human intervention).
Saving to the registry requires calling an API function, to which you pass the string you want to save. Because strings are big bulky things, we pass a pointer to them instead. VB usually hides this behind the scenes, if you declare the API parameter as a String. It will convert it to ANSI and pass the pointer itself. Although if you declare the parameter as a Long you have the option of passing the string yourself.
Now let's look at what vbNullString means. vbNullString is a constant that represents, believe it or not, a null string. As bushmobile demonstrated, a null string is not the same as an empty string, but it is in fact a null pointer. So if you pass vbNullString to an API function, you are actually passing a null pointer - which means nothing will be passed at all and the call will be ignored. If on the other hand you pass in an empty string (""), that is what will be saved to the registry.
Hope that explains it for you.
-
Jul 10th, 2006, 11:17 AM
#14
Re: [RESOLVED] String Manipulation. "" vs chr(0)
Thats such a nicely written post you deserve a rating!
-
Jul 10th, 2006, 01:58 PM
#15
Re: String Manipulation. "" vs chr(0)
Penagate !
That is anoter excellent piece of an string theory. Thank you very much.
Actually by opening this thread i have come out of confusion of char(0), nullstring & "" things really. Thank severy body.
 Originally Posted by penagate
If on the other hand you pass in an empty string (""), that is what will be saved to the registry.
But penagate i have a problem regarding above quote.
before i see your post i have already made antoher thread at http://www.vbforums.com/showthread.p...59#post2539959
in my new thread i have really explaind the problem what i have with xp 2k and 98. it seems the above qoute by you have somthing to deal with my new thread. XP / 2K Accecpts the empty string. but Windows98se doesnot?
-
Jul 10th, 2006, 04:18 PM
#16
Re: String Manipulation. "" vs chr(0)
 Originally Posted by penagate
As bushmobile demonstrated, a null string is not the same as an empty string, but it is in fact a null pointer. So if you pass vbNullString to an API function, you are actually passing a null pointer - which means nothing will be passed at all
Actually, since you're passing the address of the string, you're passing the address 0x0. Since that's almost always illegal (your program doesn't have access to that address), the API probably returns without doing anything if you pass it a "pointer" to vbNullString. (I put quotes arount the word pointer because something that doesn't exist in memory can't be pointed to.)
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jul 10th, 2006, 04:56 PM
#17
Re: [RESOLVED] String Manipulation. "" vs chr(0)
As I posted in your other thread, the answer is actually pretty simple. When you store a string value in the registry using RegSetValueEx you must specify the length of the string including the null terminating character. Since you use the Len() function there you pass 0 instead of 1 for an empty string which cause the function call to fail. I guess Win2K and XP is more forgiving if you pass zero as the length for an empty string.
When you tried Chr(0) instead VB see it as a string containing one null character since VB doesn't see it as the termination of the string. So the Len() function now returns 1 which is the correct value you want to use to store an empty string using RegSetValueEx.
So you may pass vbNullString or "" if you like as the string but the length should always be the length of the string + 1.
As already explained vbNullString is not the same thing as Chr(0). Neither is vbNullString the same thing as "", however as long as you use it directly in VB (not passing it to an API function) VB will not make any difference between "" and vbNullString and that's simply because when you compare two strings in VB you compare the text they contain and not like in C/C++ if they point to the same memory address. However RegSetValueEx will copy the text, including the terminating null character, of the string you pass and write it to the registry so it needs to know the number of characters including the null character that you want to store.
I hope this wasn't too confusing. Simply pass Len(strname) + 1 to the last argument of RegSetValueEx and you can use your origional code.
-
Jul 10th, 2006, 11:26 PM
#18
Re: [RESOLVED] String Manipulation. "" vs chr(0)
Hai a142 and jacim. thank you for the participation.
Hai jacim and everybody !!
Jacim i tried your solution.
VB Code:
lngresult=RegSetValueEx (path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname) + 1)
Result : 98SE Shows NO MERCY. .87 Returned
if i debug.print strptr(strname) in 98se the debug windw out puts 0. so strname uses no memory. if iam corrct.
'thanks bush mobile for strptr, it is somthing new for me..
So i tried this way
VB Code:
strname = Form1.text1.Text
if strname="" then strname=chr$(0)
....
....
lngresult=RegSetValueEx (path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname) + 1)
....
...
98SE Is happy and returned 0 :-)
the debug.print shown strptr(strname) somthing assume like 1909809 (not remember the numbers exactly. i am in xp now)
so i assume strname uses memory.
So jacim and others... Shall i go ahead using this way ?
-
Jul 11th, 2006, 04:58 PM
#19
Re: [RESOLVED] String Manipulation. "" vs chr(0)
 Originally Posted by Fazi
So jacim and others... Shall i go ahead using this way ?
If it works for you then go ahead.
Could you please try to spell my name correctly in at least one of your posts.
-
Jul 11th, 2006, 10:11 PM
#20
Re: [RESOLVED] String Manipulation. "" vs chr(0)
Hai every body
thank you all for participating in my thread. your solutions are really really worth.
regards
from
SA
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
|