Results 1 to 10 of 10

Thread: [RESOLVED] A question about functions

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    126

    Resolved [RESOLVED] A question about functions

    I was just wondering how functions are returned.

    If you had a function:
    VB Code:
    1. Private Function CheckSafeList(Item As String)
    2. CheckSafeList = False
    3. For t = 0 To UBound(GetSafeList)
    4.     If Trim(Item) = Trim(GetSafeList(t)) Then
    5.         CheckSafeList = True
    6.         Exit For
    7.     End If
    8. Next
    9. End Function
    Would the function terminate at the 'CheckSafeList = False', or does it run through the entire function, and return the end value?

    Thanks in advance =)

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: A question about functions

    In runs until the end function unless there is an early "exit" used... BTW, this seems to be better...

    VB Code:
    1. Private Function CheckSafeList(ByVal Item As String) As Boolean
    2.     CheckSafeList = False
    3.     For t = 0 To UBound(GetSafeList)
    4.         If Trim$(Item) = Trim$(GetSafeList(t)) Then
    5.             CheckSafeList = True
    6.             Exit For
    7.         End If
    8.     Next
    9. End Function
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: A question about functions

    Also, if you want to ignore the casing of the item then you can add a UCASE$ or LCASE$ after the Trim$ functions.
    VB Code:
    1. Private Function CheckSafeList(ByVal Item As String) As Boolean
    2.     CheckSafeList = False
    3.     For t = 0 To UBound(GetSafeList)
    4.         If Trim$(UCase$(Item)) = Trim$(UCase$(GetSafeList(t))) Then
    5.             CheckSafeList = True
    6.             Exit For
    7.         End If
    8.     Next
    9. End Function
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    126

    Re: A question about functions

    Ah ok, thanks for clearing that up.
    Was just using that as an example, but was does the Trim$ do, is it any different from Trim?

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: A question about functions

    Nope, but it is a little bit faster, as the system doesn't have to convert into a variant. It tells it that you want to return a string, and are using a string.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: A question about functions

    The $ sign designates a string type variable is going to be processed. Its a speed improvement but only minor unless you have a loop or something.
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    126

    Re: A question about functions

    Ah ok, thanks again guys =)

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

    Re: [RESOLVED] A question about functions

    Whenever you have a loop you should try and optimise it as much as possible, because loops can really slow your application down.

    Removing the If() statement and moving Trim$(UCase(Item)) outside the loop can really speed it up.
    VB Code:
    1. Private Function CheckSafeList(ByRef pItem As String) As Boolean
    2.     Dim upper As Long, i As Long
    3.     upper = UBound(GetSafeList)
    4.     Do
    5.         CheckSafeList = StrComp(pItem, GetSafeList(i), vbTextCompare)
    6.         i = i + 1
    7.     Loop While (CheckSafelist And (i <= upper))
    8. End Function

    Edit: using StrComp() is even faster, then you can remove all the Trim$(Ucase()) stuff and pass the string ByRef.
    Last edited by penagate; Nov 25th, 2005 at 05:27 AM.

  9. #9
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: [RESOLVED] A question about functions

    Just an interesting note:

    If you had (in VB.NET)
    VB Code:
    1. Private Function Blah(ByRef n as Integer) as integer
    2.   n += 5
    3.   Return n
    4.   n -= 3
    5. End Function
    6.  
    7. Dim x as integer = 5
    8. Dim y as integer = 0
    9. y = Blah(x)
    10. '// x = 10
    11. '// y = 10
    Y would be 10 and X would be 10. X would not be 7, as would happen in VB6. In VB.Net, the function ends at a return.
    VB Code:
    1. Private Function Blah(ByRef n as Integer) as integer
    2.   n = n + 5
    3.   Blah = n
    4.   n = n - 3
    5. End Function
    6.  
    7. Dim x as integer
    8. Dim y as integer
    9. x = 5
    10. y = 0
    11. y = Blah(x)
    12. '// x = 7
    13. '// y = 10

    Just interesting to know.

    r0ach™
    Don't forget to rate the post

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

    Re: [RESOLVED] A question about functions

    Yeah but in VB6 the Return keyword is used to complement GoSub. It has a completely different purpose. As has been pointed out you need to use an Exit call to quit the routine.

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