dcsimg
Results 1 to 34 of 34

Thread: [RESOLVED] Empty String

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Resolved [RESOLVED] Empty String

    Why does MsgBox below return only
    |
    not
    | |

    Code:
    Private Sub Command1_Click()
        
        Dim str As String * 32
    
        'str = ""
        
        MsgBox Len(str)
        MsgBox "|" & str & "|"
    
    End Sub

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: Empty String

    It's printing both. It's just getting confused on how to word-wrap your text, and it's printing out-of-view.

    Try this:

    Code:
    
    Private Sub Command1_Click()
        Dim str As String * 32
    
    
        MsgBox Len(str)
        MsgBox "|" & "  " & "|"
    
    End Sub
    

    Or this:

    Code:
    
    Private Sub Command1_Click()
        Dim str As String * 32
    
    
        Debug.Print Len(str)
        Debug.Print "|" & str & "|"
    
    End Sub
    
    

    Good Luck,
    Elroy

    EDIT1: Are you experimenting with out fixed-length-strings work? They are very weird animals. They're not BSTR-type strings. Rather, they're just an area of memory precisely the length of the declared string. They're initially filled with nulls (vbNullChar). And, when declared by themselves (as you have done), you can't even "truly" pass them or find them in memory.

    When you pass them as ByRef (the default), VB6 jumps through all kinds of weird hoops to pass these things. Basically, it creates a temporary BSTR string, passes that temporary string, the called procedure may change that BSTR, and then upon return that BSTR is fitted back into the fixed-length-string (with possible space-padding or truncation).

    It's weird but not even VarPtr() works as you'd think with these things. That's because, as stated, a temporary BSTR is created, and you get the pointer to this temporary BSTR.

    The only way I've found to get the true address to a fixed-length-string is to embed it into a UDT, and then get its offset within the UDT. Also, with respect to UDTs, fixed-length-strings and BSTRs work very differently. Within a UDT, a fixed-length-string is truly within the UDT, whereas a BSTR just has its four-byte-pointer in the UDT with it's string data off somewhere else.

    Personally, I've only found use for fixed-length-strings when used within a UDT. On their own, they're just a bit too strange for my taste.
    Last edited by Elroy; May 11th, 2019 at 09:03 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    OK but the following doesn't give me what I want.
    Code:
    Private Sub Command2_Click()
        
        Dim str As String * 32
    
        If RTrim(str) = "" Then
            MsgBox "empty"
        End If
    
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    I also tried
    Code:
      
        If str = vbNullString Then
            MsgBox "empty"
        End If
    but that doesn't work either

  5. #5
    Fanatic Member
    Join Date
    Dec 2014
    Posts
    810

    Re: Empty String

    because String * 32 is always 32 in length, u can't trim it
    u need to fill those 32 with something otherwise its Chr(0) and we know MsgBox dosen't handle Chr(0) well.

    If str = Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) Then MsgBox "empty"

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    OK thanks

    Is there a way to definitively check for a blank or empty string?

    I've tried
    Code:
    Private Sub Command3_Click()
        
        Dim str As String * 32
        
        'str = ""
        
        If RTrim(str) = "" Then
            MsgBox "empty1"
        End If
    
        If str = vbNullString Then
            MsgBox "empty2"
        End If
        
        If RTrim(str) = vbNullString Then
            MsgBox "empty3"
        End If
        
        If LenB(str) = 0 Then
            MsgBox "empty4"
        End If
        
    End Sub

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: Empty String

    Hi mms,

    Yes, baka outlined your problem.

    And, to repeat my edited comment, "Personally, I've only found use for fixed-length-strings when used within a UDT. On their own, they're just a bit too strange for my taste." You may want to take that advise as well. From post #3 and #4, it truly looks like you're after a variable-length BSTR-type string. They will work more like you're wanting them to.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    The str I'm checking for is actually in a UDT

  9. #9
    Fanatic Member
    Join Date
    Dec 2014
    Posts
    810

    Re: Empty String

    what you can do is:

    Private Empty32 as String * 32

    Dim str As String * 32
    if str = Empty32 then msgbox "empty"

    if u change str with anything, like str = " " it will not be equal to Empty32
    when you want to erase str, you can not do str = vbnullstring, but you can str = Empty32,
    that will fill str with 32 zeros

  10. #10
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,279

    Re: Empty String

    Something like this should work to find "empty" strings (fixed-length or otherwise). You may want to change the what you consider "empty" characters though (see note in comments):

    Code:
    Public Function IsEmptyOrWhitespaceString(p_String As String) As Boolean
       Dim ii As Long
       
       ' Loop through all characters to see if string is empty
       For ii = 1 To Len(p_String)
          Select Case AscW(Mid$(p_String, ii, 1))
          Case 0, 32  ' You may want to consider other characters as "empty", e.g. Tab, CR, LF, etc...
                      ' or maybe even all the Unicode whitespace characters?
                      ' See: https://en.wikipedia.org/wiki/Whitespace_character
          Case Else
             ' Not empty
             Exit For
          End Select
       Next ii
       
       IsEmptyOrWhitespaceString = ii > Len(p_String)
    End Function

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: Empty String

    You could write some wrapper function for your comparisons, like this:

    Code:
    
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim str As String * 32
    
        str = "asdf"
        str = vbNullString
    
        Debug.Print TrimNullAndSpace(str) = vbNullString
    
    
    End Sub
    
    Public Function TrimNullAndSpace(ByVal s As String) As String
        Do
            If Right$(s, 1&) <> vbNullChar Then Exit Do
            s = Left$(s, Len(s) - 1&)
        Loop
        TrimNullAndSpace = Trim$(s)
    End Function
    
    
    

    But I certainly wouldn't do that. You're creating all kinds of temporary BSTR strings to use that, when you should just be using a BSTR (variable-length-string) in the first place.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: Empty String

    Quote Originally Posted by baka View Post
    what you can do is:

    Private Empty32 as String * 32

    Dim str As String * 32
    if str = Empty32 then msgbox "empty"

    if u change str with anything, like str = " " it will not be equal to Empty32
    when you want to erase str, you can not do str = vbnullstring, but you can str = Empty32,
    that will fill str with 32 zeros

    Baka, you've got to be careful here. These crazy fixed-length-strings are initialized as nulls (vbNullChar, Chr$(0)). However, once you start using them, they tend to pad with spaces rather than nulls. So, it's very easy for your "empty" comparisons to still not work.


    EDIT1: And, we could do something like this, which would be a bit tricky to work around:

    Code:
    
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim str As String * 32
    
        str = " " & vbNullChar & " " & vbNullChar & " " & vbNullChar
    
        ' Now, it's going to be tricky to figure out whether you want to call that "empty" or not.
    
    End Sub
    
    
    
    
    Last edited by Elroy; May 11th, 2019 at 09:14 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    OK thanks to all.

    I will have to find another way to achieve my goal.

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: Empty String

    Quote Originally Posted by mms_ View Post
    OK thanks to all.

    I will have to find another way to achieve my goal.
    Just use a variable length string.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  15. #15
    Fanatic Member
    Join Date
    Dec 2014
    Posts
    810

    Re: Empty String

    always work, Im using this method and its working well.
    when creating a string its always 0.
    so Empty32 will always be 32 zeros.
    while str that we gonna use, can be emptied just by coping to this 32 zeros that we never change. theres nothing to worry about that, its simply math.
    also, fixed-length-string are not crazy at all, its like saying Byte are crazy because we can only use 0-255. but its the boundaries, quite simple.
    non-fixed length strings are more crazy.
    but sure, a fixed-length-string will always be a certain length and we need to consider that in our operations.

    as for comparison. if you don't know how to code it, of course it can be messy.
    but if you "understand" that its a defined length and all positions (like an array) need to be filled. its quite easy.
    if you can't handle it, better work with non-fixed-length.

    and mms_ we gave u tons of ways, and even so u say u need to find another way?
    dont understand this at all. maybe u need to explain better, exactly what you want to do.
    a UDT with a fixed length string is always 0xlen and if you want to know its empty (never changed) i already gave u the solution.
    if you don't know the length of the string, jpbro and similar functions to loop through the entire string can check if its empty or not.
    i believe we can also do a memory scan, using API to check if the string have "00" or not, by checking its memory position, if we really want that.
    if its "enough" to check the first letter (position 1) if its 0 or 32 we can do that as well. it all depends the accuracy and what empty means for u, as jpbro explained.
    Last edited by baka; May 11th, 2019 at 09:25 AM.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    baka

    You are correct
    Your method works perfectly!!

    Code:
    Private Sub Command4_Click()
        
        Dim str As String * 32
        Dim Empty32 As String * 32
        
        If str = Empty32 Or str = "" Then MsgBox "empty"
        
    End Sub
    I don't know why it did not work the first time I tried it, but no matter it works!!

    Thank you!!
    Last edited by mms_; May 11th, 2019 at 10:01 AM.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    Oh geez

    Why doesn't this work?
    Code:
    Private Sub Command4_Click()
        
        Dim str As String * 32
        Dim Empty32 As String * 32
        
        str = ""
        
        If str = Empty32 Or str = "" Then MsgBox "empty"
        
    End Sub

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: Empty String

    Nevermind

    This works! (covering both eventualities)
    Code:
    Private Sub Command4_Click()
        
        Dim str As String * 32
        Dim Empty32 As String * 32
        
        str = " " 'commented in or out
        
        If str = Empty32 Or RTrim(str) = "" Then MsgBox "empty"
        
    End Sub
    Thany you!

    I will have to study jpbro's code to better understand
    Last edited by mms_; May 11th, 2019 at 10:41 AM.

  19. #19
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: Empty String

    Quote Originally Posted by baka View Post
    fixed-length-string are not crazy at all
    I didn't mean to imply that they're unpredictable, random, or buggy. It's just that they have some unusual behavior:

    • Initialized as vbNullChar but padded using spaces during use.
    • When passing them ByRef, VB6 jumps through all kinds of hoops to create a temporary BSTR, and then, upon return, unwinds those hoops to get any changes back into the fixed-length-string.
    • That, when not in a UDT, it's virtually impossible to get their memory address.

    Sure, if we keep those things in mind, we can use them with confidence.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  20. #20
    Fanatic Member
    Join Date
    Dec 2014
    Posts
    810

    Re: [RESOLVED] Empty String

    the first one dont work because:
    str = "" will actually do str = chr(32) + 31x0

    so when you check str = "" it will not trigger because u forgot 31x0, but if you do left$(str,1) = chr(32) it will trigger.

    trim will return a string, not a fixed-length string.
    if str is default (not changed), it will do a copy, and its quite strange trim do that, if you trim str = 1x0 & chr(32) + 30x0 it will return a vbnullstring but if you trim 32x0 it will return 32x0

    anyway, str = Empty32 or Trim(str) = vbnullstring should cover everything.

    true Elroy,
    they can be a hassle, but sometimes they are actually quite handy.
    also, why do u want to initialize them at all? they are 0 by default?
    and of course byref should return to a fixed-length, since its that we have.
    sure, if you do something in that function that will go outside its boundaries they will get discarded.
    what you need to keep in mind is that a fixed-length is like an array and its fixed.
    if you change array(0) and think it should apply to array(31) you are doing it wrongly.
    Last edited by baka; May 11th, 2019 at 10:54 AM.

  21. #21
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,279

    Re: [RESOLVED] Empty String

    Although it's not really "a bug" or anything in the posted code-snippets...
    I'd nevertheless try to avoid variable-names which "cover" built-in runtime-functions (meaning Str())

    And BTW - Instead of Empty32 one could use the generic:
    String$(Len(FixedStr), 0)
    in comparisons.

    Or just convert all NullChars to spaces (so that the Trim-Functions could be used normally on such fixed-length-strings):
    Replace(FixedStr, vbNullChar, " ")

    Olaf

  22. #22
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: [RESOLVED] Empty String

    Quote Originally Posted by baka View Post
    trim will return a string, not a fixed-length string
    Nothing will return a fixed-length string. And nothing will receive a fixed-length string as an argument (although it may appear so when not looking closely) ... and this is true regardless of whether the procedure is a built-in VB6 procedure or a procedure (Sub, Function, Property) we've created, or even an API call.

    The "true" use of fixed-length strings (without any temporary creation/conversion to a BSTR) is limited to the scope in which the fixed-length string is declared.

    EDIT1: Ok, again, if not in a UDT.
    Last edited by Elroy; May 11th, 2019 at 10:52 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: [RESOLVED] Empty String

    So this I could consider final?
    Code:
    Private Sub Command5_Click()
        
        Dim myStr As String * 32
        Dim Empty32 As String * 32
        
        'myStr = ""   'commented in or out
        
        If myStr = Empty32 Or Trim(myStr) = vbNullString Then MsgBox "empty"    
    
    End Sub
    Last edited by mms_; May 11th, 2019 at 10:58 AM.

  24. #24
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: [RESOLVED] Empty String

    The only got'cha would be what I suggested in post #12, which would be highly unlikely:

    Code:
    
    myStr = " " & vbNullChar & " " & vbNullChar & " " & vbNullChar
    
    Also, please use Trim$() and not Trim(), so you don't go through a Variant to get your work done.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  25. #25
    Fanatic Member
    Join Date
    Dec 2014
    Posts
    810

    Re: [RESOLVED] Empty String

    Elroy, u are not correct.

    try:
    Dim str As String * 32
    str = Trim(str)
    str = "A"
    MsgBox Len(str)

    will return the length of 32,
    meaning Trim will return a fixed-length string, or just passing it.

    mms_
    yes that should cover it.
    but only if you know the length of the string, otherwise the example of Schmidt is good. that will create a temporary Empty*Len

    of course we can always mess things up, that space + nullchar + space etc.
    if someone is adding that to the string * 32 it means its there for a reason, its not considered empty.
    when handling strings in a UDT, and we want to clean it, we usually zero it, like Schmidt example, we zero the entire length, like we do with zeromemory.

    if I want to know if a fixed-length-string is "not used", i just check if its zeroed. even space for me its not empty but something, its ascii32.
    Last edited by baka; May 11th, 2019 at 11:09 AM.

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: [RESOLVED] Empty String

    OK thanks

    Trim$ vs Trim is just a speed issue thing?

  27. #27
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: [RESOLVED] Empty String

    Quote Originally Posted by mms_ View Post
    OK thanks

    Trim$ vs Trim is just a speed issue thing?
    Yes.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: [RESOLVED] Empty String

    Not sure if I understood Schmidt correctly in Post #21,
    but the following only works if
    myStr = "" is commented OUT


    Code:
    Private Sub Command8_Click()
    
        Dim myStr As String * 32
        'myStr = ""   
        
        If myStr = String$(Len(myStr), 0) Or myStr = vbNullString Then MsgBox "empty"
        
    End Sub
    Last edited by mms_; May 11th, 2019 at 12:26 PM.

  29. #29
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,279

    Re: [RESOLVED] Empty String

    When I have to work with Fixed-Strings, I have some helper-function similar to the one below anyways
    - to be able to concat content of fixed-member-strings with normal strings
    - or to parse them "till ZeroTerminators" after returning from some API-call

    Code:
    Private Sub Form_Load()
      Dim strF As String * 32
    
          strF = "" 'comment in or out
     
      Debug.Print Len(F2S(strF)), "[" & F2S(strF) & "]"
    End Sub
    
    Public Function F2S(strF As String) As String
      F2S = RTrim$(Left$(strF, InStr(strF & vbNullChar, vbNullChar) - 1))
    End Function
    So, when something like the above F2S() is in place anyways, one can use it to do empty-checks as usual:
    If Len(F2S(strF)) = 0 Then Debug.Print "empty"

    <shrug>

    Olaf

  30. #30
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: [RESOLVED] Empty String

    Quote Originally Posted by baka View Post
    Elroy, u are not correct.

    try:
    Dim str As String * 32
    str = Trim(str)
    str = "A"
    MsgBox Len(str)

    will return the length of 32,
    meaning Trim will return a fixed-length string, or just passing it.
    Ok, this isn't the easiest thing to prove, and a complete proof may need to be provided by a dis-assembly by The Trick.

    However, here's enough proof for me. You will need to study this code carefully, and read my comments as well:

    Code:
    
    Option Explicit
    '
    Private Declare Function GetMem8 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
    '
    Private Type UdtType
        sFixed1 As String * 8
        sFixed2 As String * 8
    End Type
    
    Private Sub Form_Load()
        Dim u As UdtType
        Dim bb() As Byte
        Dim s As String
    
        ReDim bb(7)
    
    
        u.sFixed1 = "asdfasdf"              ' Give our sFixed1 a value.
        u.sFixed2 = "qwerqwer"              ' Give our sFixed2 a value.
    
    
    
    
        Debug.Print "----- sFixed1 test:"
        '
        Debug.Print VarPtr(u)                   ' This is truly the address of sFixed1 as it's first in the UDT.
        GetMem8 ByVal VarPtr(u), bb(0&)         ' Now, let's move sFixed1 to a byte array.
        s = bb                                  '   and then move it into a BSTR.
        Debug.Print s                           ' We see that using VarPtr(u), we can "get at" sFixed1, so VarPtr(u) is correct.
        '
        Debug.Print VarPtrOfArg(u.sFixed1)      ' Reports a DIFFERENT value than VarPtr(u).
                                                ' This is because u.sFixed1 was NOT passed, but instead a temporary BSTR version of it.
    
    
        ' And, for grins, we'll do it all again with u.sFixed2:
        Debug.Print "----- sFixed2 test:"
        '
        Debug.Print VarPtr(u) + 8&              ' This is truly the address of sFixed2 as it's first in the UDT.
        GetMem8 ByVal VarPtr(u) + 8&, bb(0&)    ' Now, let's move sFixed2 to a byte array.
        s = bb                                  '   and then move it into a BSTR.
        Debug.Print s                           ' We see that using VarPtr(u)+8&, we can "get at" sFixed2, so VarPtr(u)+8& is correct.
        '
        Debug.Print VarPtrOfArg(u.sFixed1)      ' Reports a DIFFERENT value than VarPtr(u) + 8&.
                                                ' In fact, INTERESTINGLY, this reports the same number as the first time we called VarPtrOfArg.
                                                ' How can u.sFixed2 have the same address as u.sFixed1????
                                                ' They DON'T, but the temporary BSTR actually DOES wind up using the same memory!!!
    
    
    End Sub
    
    Private Function VarPtrOfArg(s As String) As Long
        ' A fixed-length string is NOT passed in.
        ' Rather, a temp BSTR of the fixed-length string is passed in instead.
        ' Although, if we were to make changes to s, they would be reflected in the fixed-length string that appeared to be passed.
        VarPtrOfArg = VarPtr(s)
    End Function
    
    

    To illustrate, I had to place the fixed-string in a UDT, so I could actually get its memory address.

    Again, you can NOT pass fixed-strings as arguments.

    Just as some examples:

    Code:
    
    i = VarPtr(SomeFixedLengthString)       ' A temp BSTR is created before VarPtr is called.
    s = Trim$(SomeFixedLengthString)        ' A temp BSTR is created before Trim$ is called, and a BSTR is returned.
    i = VarPtrOfArg(SomeFixedLengthString)  ' A temp BSTR is created before VarPtrOfArg is called.
    i = Len(SomeFixedLengthString)          ' A temp BSTR is created before Len is called.
    s = Left$(SomeFixedLengthString, 2&)    ' A temp BSTR is created before Left$ is called, and a BSTR is returned.
    
    
    s = Left$(s, Len(s) - 1&)               ' Where s is some fixed-length string.
    ' For Len(s), a temp BSTR is created before Len is called.
    ' For Left$(...), another temp BSTR is created before Left$ is called.
    ' And then finally, another BSTR is created for the return of Left$(...).
    

    Baka, Take Care,
    Elroy

    EDIT1: And, when a fixed-length string is passed as an argument (or, rather the temporary BSTR is passed), and it's passed ByRef, and it's changed by the called procedure ... upon return, that temporary BSTR is used to patch up the value of the original fixed-length string. And this is all done by the compiler. For me, that qualifies using the word "weird". It also shows that using these things is very inefficient if you intend to use them as arguments.

    Again, I recommend not using them unless they're in a UDT. For certain purposes, they're quite useful when in a UDT.
    Last edited by Elroy; May 11th, 2019 at 12:40 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: [RESOLVED] Empty String

    Schmidt
    Yes this works

    What does F2S stand for so that I might better understand this?
    Fixed To String perhaps?
    The function is converting a fixed length string to a standard string?
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim myStr As String * 32
        
        'myStr = " "   'comment in or out
    
        If Len(F2S(myStr)) = 0 Then MsgBox "empty"
    
    End Sub
    
    Public Function F2S(strF As String) As String
      F2S = RTrim$(Left$(strF, InStr(strF & vbNullChar, vbNullChar) - 1))
    End Function

  32. #32
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,552

    Re: [RESOLVED] Empty String

    mms,

    Olaf's function is really more of a "trim" function that just does what your post #23 does. If you've paid attention to my posts, you'll realize that your fixed-length strings are always converted to "regular" (BSTR) strings whenever you use them as an argument. And everything returns a BSTR (not fixed-length string).

    But, when you assign a fixed-length string, it pads or truncates to maintain its length.

    Good Luck,
    Elroy

    EDIT1: And (sorry if this is a hi-jack) I challenge anyone to show me that I'm wrong about not being about to pass a fixed-length string. Baka, your example didn't really show anything (sorry).
    Last edited by Elroy; May 11th, 2019 at 12:58 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    148

    Re: [RESOLVED] Empty String

    OK thanks

    And (sorry if this is a hi-jack)...
    No worries

  34. #34
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,279

    Re: [RESOLVED] Empty String

    Quote Originally Posted by Elroy View Post
    Olaf's function is really more of a "trim" function...
    It does a bit more than that, because:
    - it detects (and cuts-out) the string until the first Zero-Terminator-Char
    - which handles the part, when an API-call has placed a zero-terminated C-string within the fixed-string-block
    - also handled is the part of a "virginal" VB6-fixed-string which was not yet initialized - and contains zeroes throughout
    - but it also removes any "space-padding at the right-hand-side" (which happens, when you assign shorter strings to it)

    @mms
    Yes, F2S was meant as "Fixed to String".

    Olaf

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