Results 1 to 20 of 20

Thread: [RESOLVED] String Manipulation. "" vs chr(0)

  1. #1

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Resolved [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)

  2. #2
    Addicted Member
    Join Date
    Mar 2006
    Posts
    178

    Re: String Manipulation. "" vs chr(0)

    "" is an empty string

    It's better programming practice to use vbNullString in place of ""

  3. #3

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: String Manipulation. "" vs chr(0)

    Thank you for the posts.

    So if a "" returns then we have to conver it as

    VB Code:
    1. if strval="" then strval=chr(0)

    Is this the way?

  4. #4
    Member Max bayne's Avatar
    Join Date
    Jul 2006
    Location
    Egypt
    Posts
    59

    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

  5. #5

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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.

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Sub Form_Load()
    2.     Dim s1 As String, s2 As String, s3 As String
    3.     s1 = Chr$(0)
    4.     s2 = ""
    5.     s3 = vbNullString
    6.    
    7.     Debug.Print Len(s1), Len(s2), Len(s3)
    8.     Debug.Print StrPtr(s1), StrPtr(s2), StrPtr(s3)
    9. End Sub
    both the first two exist in memory, whereas the third uses no memory.

  7. #7

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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

  8. #8

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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.

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: String Manipulation. "" vs chr(0)

    Quote Originally Posted by Fazi
    So text boxes with no text always returns ""
    Well, if we test it:
    VB Code:
    1. Dim s As String
    2.     s = Text1.Text
    3.     Debug.Print StrPtr(s)
    we get 0, so Text1.Text returns vbNullString if empty, not ""

    Quote 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.

  10. #10

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Talking Re: String Manipulation. "" vs chr(0)

    Quote Originally Posted by bushmobile
    Well, if we test it:
    VB Code:
    1. Dim s As String
    2.     s = Text1.Text
    3.     Debug.Print StrPtr(s)
    we get 0, so Text1.Text returns vbNullString if empty, not ""
    Bush.. Thank you for the point.

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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...

  12. #12

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Talking Re: [RESOLVED] String Manipulation. "" vs chr(0)

    Quote 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.

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String Manipulation. "" vs chr(0)

    Quote 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.

  14. #14
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [RESOLVED] String Manipulation. "" vs chr(0)

    Thats such a nicely written post you deserve a rating!

  15. #15

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Talking 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.

    Quote 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?

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: String Manipulation. "" vs chr(0)

    Quote 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

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  18. #18

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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:
    1. 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:
    1. strname = Form1.text1.Text
    2. if strname="" then strname=chr$(0)
    3. ....
    4. ....
    5.    lngresult=RegSetValueEx (path, "Search Now", 0, REG_SZ, ByVal strname, Len(strname) + 1)
    6. ....
    7. ...

    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 ?

  19. #19
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] String Manipulation. "" vs chr(0)

    Quote 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.

  20. #20

    Thread Starter
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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
  •  



Click Here to Expand Forum to Full Width