Return True

G

Guest

Hi.

I have a Sub that calls a Boolean Function and exits the Sub if the Return
value is False. The problem is it's exiting the Sub even if the Return value
is True.

Here's my code.

Private MySub
If Not DownloadFIIDSFile() Then Exit Sub
'---- Other code here
End Sub

Private Function DownloadFIIDSFile() As Boolean
Try
'---- Some FTP commands

'--- Check that a file was downloaded
Dim sDirs() As String
sDirs = Directory.GetFiles(DownloadPath)
iCnt = sDirs.GetLength(0)
If iCnt = 0 Then Return False
'--- Some other code
Return True
Catch e as Exception
'--- Som e error handling
End Try
End Function

I played around with the code e.g.
Dim bOK as Boolean
bOK = DownloadFIIDSFile()
If bOK = False Then Exit Sub '******** bOK is True but it still went
to execute
'******** "Exit Sub"
statement.

Any suggestions will be greatly appreciated!

Rita
 
J

Joshua Flanagan

You will probably have to include more code for anyone to have a chance
at guessing. For example, what does the function return if an exception
is caught? Do you have a Return statement in the exception handler? If
not, VB will probably return False (the default value for a boolean).

My guess is that something unexpected is happening in the code. It
throws an exception, and the exception handler runs. There is no Return
statement, so the function returns the default. And you are never the
wiser, because you caught and ignored the exception.
This a good example of why you should very rarely use "Catch e as
Exception" within a method. See my mini-rant here (and the linked
article which actually contains useful information).
http://flimflan.com/blog/archive/2004/05/20/172.aspx

Joshua Flanagan
 
O

Olaf Baeyens

I do not see a return value after the catch.
You return a value in the case you do not have an expection, but if you do
have an exception, your return value is undefined when I look at your code.
So my guess is that you get an exception and then your result is undefned.
 
G

Guest

Thanks to all for your responses. I'm out of town for 2 weeks and will post
more code/respond to the replies when I get back.
 
G

Guest

Thanks Joshua. What you described is what happened. I checked out Jason
Clark's article and will be making some changes!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top