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
Printable View
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
You cannot have a function that returns two different things. If you can explain a bit, probably someone can suggest a better solution.
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.
You could pass an array as a reference and return a boolean like in this example:
@Bornish: Oh. Hi. Didn't see your post, when replying. :)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)
Good one :thumb:Quote:
Originally Posted by Bornish
This was on my mind too, but I wanted to listen to fist what the actual requirement is.
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
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.Quote:
Originally Posted by wildfrog
This is my code and its always returning a "Not" messagebox:Quote:
Originally Posted by Thread1
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
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?
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.
Can you post the code here, so that we can take a look at it.