Results 1 to 3 of 3

Thread: I dont understand this code.. help??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Hi all..

    Im Sure something can explain some code I got
    out of this site. Its a function by Yonatan , he
    to be on a whole different level of vb then me.

    So could someone explain this in ALOT of depth??

    Code:
    Option Explicit
    
    ' Function version and sub version... Use the one which looks more fun! 
    #Const FunctionVersion = True ' Change to False to use the Sub version
    
    #If FunctionVersion Then
    Function TrimNulls(ByVal sString As String) As String
    #Else
    Sub TrimNulls(sString As String)
    #End If
        Dim lPos As Long
        
        lPos = InStr(sString, vbNullChar)
        
        If lPos > 0 Then sString = Left(sString, lPos - 1)
    #If FunctionVersion Then
        TrimNulls = sString
    End Function
    #Else
    End Sub
    #End If

  2. #2
    Guest
    The hash characters (#) are conditional compilation markers. Basically, the code can be written two ways, ad either a sub or a function.

    For a function (which would be more useful, you need to either set #Const FuctionVersion = True, or trim the excess code you don't won't.

    Therefore the function version would look more like:

    Code:
    Function TrimNulls(ByVal sString As String) As String
        Dim lPos As Long
        
        lPos = InStr(sString, vbNullChar)
        
        If lPos > 0 Then sString = Left(sString, lPos - 1)
    
        TrimNulls = sString
    End Function
    As to what the function does, well, it trims the null characters off the end of the string


    Regardingthe use of conditional compilation characters, you will often see the hash used for function declaration /w 16 and 32it functions eg:
    Code:
    #If IsWin32 then
        Declare Function FooBar() as Long
    #Else
        Declare Function FooBar() as Integer
    #End If
    This tells the compiler to check what the IsWin32 function returns (theoretically true if Win95 and greater or false in Win 3.11), and then uses the correct declare statement (this is necessary cos the declarations of long are different b/w windows versions)

    Hope that helped a bit

    - gaffa

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Oh wow.

    Thats not that bad. It just looked weird
    sense ive never seen it done before..

    Thanks alot!

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