[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.
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
Re: Incredibly dumb question, but...
Quote:
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?
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.
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
Re: Incredibly dumb question, but...
Thanks for the answers guys.
Re: Incredibly dumb question, but...
Quote:
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
Re: Incredibly dumb question, but...
Quote:
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.
Re: Incredibly dumb question, but...
Quote:
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. :thumb:
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