|
-
Nov 25th, 2005, 03:06 AM
#1
Thread Starter
Lively Member
[RESOLVED] A question about functions
I was just wondering how functions are returned.
If you had a function:
VB Code:
Private Function CheckSafeList(Item As String)
CheckSafeList = False
For t = 0 To UBound(GetSafeList)
If Trim(Item) = Trim(GetSafeList(t)) Then
CheckSafeList = True
Exit For
End If
Next
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 =)
-
Nov 25th, 2005, 03:15 AM
#2
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:
Private Function CheckSafeList(ByVal Item As String) As Boolean
CheckSafeList = False
For t = 0 To UBound(GetSafeList)
If Trim$(Item) = Trim$(GetSafeList(t)) Then
CheckSafeList = True
Exit For
End If
Next
End Function
-
Nov 25th, 2005, 03:19 AM
#3
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:
Private Function CheckSafeList(ByVal Item As String) As Boolean
CheckSafeList = False
For t = 0 To UBound(GetSafeList)
If Trim$(UCase$(Item)) = Trim$(UCase$(GetSafeList(t))) Then
CheckSafeList = True
Exit For
End If
Next
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 25th, 2005, 03:31 AM
#4
Thread Starter
Lively Member
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?
-
Nov 25th, 2005, 03:35 AM
#5
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.
-
Nov 25th, 2005, 03:35 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 25th, 2005, 03:41 AM
#7
Thread Starter
Lively Member
Re: A question about functions
Ah ok, thanks again guys =)
-
Nov 25th, 2005, 05:23 AM
#8
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:
Private Function CheckSafeList(ByRef pItem As String) As Boolean
Dim upper As Long, i As Long
upper = UBound(GetSafeList)
Do
CheckSafeList = StrComp(pItem, GetSafeList(i), vbTextCompare)
i = i + 1
Loop While (CheckSafelist And (i <= upper))
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.
-
Nov 25th, 2005, 05:37 AM
#9
Fanatic Member
Re: [RESOLVED] A question about functions
Just an interesting note:
If you had (in VB.NET)
VB Code:
Private Function Blah(ByRef n as Integer) as integer
n += 5
Return n
n -= 3
End Function
Dim x as integer = 5
Dim y as integer = 0
y = Blah(x)
'// x = 10
'// 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:
Private Function Blah(ByRef n as Integer) as integer
n = n + 5
Blah = n
n = n - 3
End Function
Dim x as integer
Dim y as integer
x = 5
y = 0
y = Blah(x)
'// x = 7
'// y = 10
Just interesting to know.
r0ach™
Don't forget to rate the post
-
Nov 25th, 2005, 05:39 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|