|
-
Nov 6th, 2006, 09:53 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Incredibly dumb question, but...
If I declare a function as this:
VB Code:
Public Function GetRunDate()
it returns a variant, right? Basically I'm wondering what the diff is between the above and the below:
VB Code:
Public Function GetRunDate() As String
I told you it was stupid; I feel really dumb asking it.
-
Nov 6th, 2006, 09:56 AM
#2
Re: Incredibly dumb question, but...
Correct. Here's how it looks:
VB Code:
Option Explicit
Private Sub Form_Load()
Debug.Print TypeName(test1())
Debug.Print TypeName(test2())
End Sub
Public Function test1()
'you can leave this empty...
End Function
Public Function test2() As String
'-||-
End Function
-
Nov 6th, 2006, 09:58 AM
#3
Thread Starter
Hyperactive Member
Re: Incredibly dumb question, but...
 Originally Posted by gavio
Correct. Here's how it looks:
VB Code:
Option Explicit
Private Sub Form_Load()
Debug.Print TypeName(test1())
Debug.Print TypeName(test2())
End Sub
Public Function test1()
'you can leave this empty...
End Function
Public Function test2() As String
'-||-
End Function
So my first example returns a variant and the second returns a string, right? I'm just wondering what the implications are for memory management; my app isn't that big but I'd like it to be as efficient as possible. Should I bother changing my functions?
-
Nov 6th, 2006, 10:05 AM
#4
Re: Incredibly dumb question, but...
IMO its ethics that we tend to follow.If a function doesn't have to return anything then don't worry about the return type else i think it should.
ps:
Public Function test2() As String
'-||-
End Function
Are you sure it will return a string ??? coz i think it wouldn't.
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Nov 6th, 2006, 10:26 AM
#5
Hyperactive Member
Re: Incredibly dumb question, but...
Variants are very slow and I believe the largest variable type you can use in VB6.
I'm not sure how strings are handled as return types, as they're arrays
-
Nov 6th, 2006, 10:29 AM
#6
Thread Starter
Hyperactive Member
Re: Incredibly dumb question, but...
Thanks for the answers guys.
-
Nov 6th, 2006, 10:38 AM
#7
Re: Incredibly dumb question, but...
 Originally Posted by litlewiki
Are you sure it will return a string ??? coz i think it wouldn't.
VB Code:
Option Explicit
Private Sub Form_Load()
'any type but String or Variant will cause the "Type mismatch" error
Dim someVar As Integer 'for example
someVar = test()
End Sub
Public Function test() As String
'...
End Function
-
Nov 6th, 2006, 11:15 AM
#8
Re: Incredibly dumb question, but...
 Originally Posted by disruptivehair
If I declare a function as this:
VB Code:
Public Function GetRunDate()
it returns a variant, right? Basically I'm wondering what the diff is between the above and the below:
VB Code:
Public Function GetRunDate() As String
I told you it was stupid; I feel really dumb asking it.
2nd makes debugging easier since your sure the function returns a string.
-
Nov 6th, 2006, 11:18 AM
#9
Thread Starter
Hyperactive Member
Re: Incredibly dumb question, but...
 Originally Posted by leinad31
2nd makes debugging easier since your sure the function returns a string.
Cool. I have a function that returns an array of strings and if I declare it 'as string' it goes nuts, so I left it as a variant but I changed the others that I could find.
-
Nov 6th, 2006, 11:32 AM
#10
Re: [RESOLVED] Incredibly dumb question, but...
If your receiving the array with a dynamic array of corresponding data type then the only problem you would have is when an array that has not been dimmed is returned by the function... code in the calling procedure probably expected an array with elements.
Rather than return just an array, you could receive an array ByRef instead in the arguments list... and the function would then return Success/Failed flags in addition to processing/populating/dimensioning/etc the passed array.
VB Code:
Dim passedArray() As String
If SampleProc(passedArray) Then
debug.print passedarray(0)
Else
'don;t process undimmed array to avoid error
End If
'-------
Public Function SampleProc(ByRef receivedArray() As String) As Boolean
Redim receivedArray(1)
receivedArray(1) = "hello"
receivedArray(1) = "world"
SampleProc = True
End Function
Last edited by leinad31; Nov 6th, 2006 at 11:37 AM.
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
|