-
May 16th, 2007, 09:18 AM
#1
Function Return Types
I am making a function and I want it to return an array if it is a success otherwise a boolean of false. Is there a way I can do this?
Function test()
If ( mystuff = Good) Then
Return myArray
Else
Return False
End If
End Function
-
May 16th, 2007, 09:31 AM
#2
Re: Function Return Types
You cannot have a function that returns two different things. If you can explain a bit, probably someone can suggest a better solution.
-
May 16th, 2007, 09:43 AM
#3
Re: Function Return Types
You can always return a structure that contains both the boolean, lets call it isArrayValid, and the array... thus, after the function returns, you can test if the array content is valid and can be used.
Still, I'd recommend passing the array as an "out" parameter (ByRef) for the function... which returns the status as a boolean, or even an error code, which enables the caller to find out what went wrong when the array content isn't valid.
-
May 16th, 2007, 09:46 AM
#4
Re: Function Return Types
You could pass an array as a reference and return a boolean like in this example:
Code:
Function DoIt(ByRef A() As Integer) As Boolean
ReDim A(2)
A(0) = 1
A(1) = 2
DoIt = True
End Function
dim Arr() as integer
If DoIt(Arr) Then debug.print Arr(0), Arr(1)
@Bornish: Oh. Hi. Didn't see your post, when replying.
-
May 16th, 2007, 09:47 AM
#5
Re: Function Return Types
Originally Posted by Bornish
You can always return a structure that contains both the boolean, lets call it isArrayValid, and the array... thus, after the function returns, you can test if the array content is valid and can be used.
Still, I'd recommend passing the array as an "out" parameter (ByRef) for the function... which returns the status as a boolean, or even an error code, which enables the caller to find out what went wrong when the array content isn't valid.
Good one
This was on my mind too, but I wanted to listen to fist what the actual requirement is.
-
May 16th, 2007, 09:16 PM
#6
Re: Function Return Types
or you may declare a variant-returning function and use vartype to determine the return value
Code:
private sub callingSub()
dim ret as variant
ret = testFunction()
if vartype(ret) = vbboolean then
' boolean
else
' array or other type
end if
end sub
private function testFunction() as variant
' some codes
end function
Busy
-
May 16th, 2007, 10:51 PM
#7
Re: Function Return Types
Maybe it's even possible to return Nothing. So, when the function fails return Nothing, and when the function succeeds return the array (possibly an empty one).
- petter
-
May 16th, 2007, 11:07 PM
#8
Re: Function Return Types
Originally Posted by wildfrog
Maybe it's even possible to return Nothing. So, when the function fails return Nothing, and when the function succeeds return the array (possibly an empty one).
- petter
Nothing will not work for Arrays in VB 6. Thread1's solution is a better one using Variants.
-
May 17th, 2007, 07:59 AM
#9
Re: Function Return Types
Originally Posted by Thread1
or you may declare a variant-returning function and use vartype to determine the return value
Code:
private sub callingSub()
dim ret as variant
ret = testFunction()
if vartype(ret) = vbboolean then
' boolean
else
' array or other type
end if
end sub
private function testFunction() as variant
' some codes
end function
This is my code and its always returning a "Not" messagebox:
Code:
Private function testFunction() As Variant
testFunction = False
End function
private sub callingSub()
dim ret as variant
ret = testFunction()
if (vartype(ret) = vbboolean) then
MsgBox "Boolean"
else
MsgBox "Not"
end if
end sub
Last edited by danbopes; May 17th, 2007 at 08:08 AM.
-
May 17th, 2007, 08:06 AM
#10
Re: Function Return Types
if you see your code closely, the name of the function that you have written is textFunction while as in the CallingSub procedure you are calling ret = testFunction().
Does your code Execute properly. Doesn't it throw any error?
-
May 17th, 2007, 08:19 AM
#11
Re: Function Return Types
Yeah I figured it out, for some reason my ADODB result set it always returning false for result.EOF even if there is no rows.
-
May 17th, 2007, 09:35 AM
#12
Re: Function Return Types
Can you post the code here, so that we can take a look at it.
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
|