Return True

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RitaG

    #1

    Return True

    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 DownloadFIIDSFi le() Then Exit Sub
    '---- Other code here
    End Sub

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

    '--- Check that a file was downloaded
    Dim sDirs() As String
    sDirs = Directory.GetFi les(DownloadPat h)
    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 = DownloadFIIDSFi le()
    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


  • Joshua Flanagan

    #2
    Re: Return True

    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).


    Joshua Flanagan

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Return True

      RitaG <RitaG@discussi ons.microsoft.c om> wrote:[color=blue]
      > 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.[/color]

      Could you post a short but complete program which demonstrates the
      problem?

      See http://www.pobox.com/~skeet/csharp/complete.html for details of
      what I mean by that.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Cor Ligthert

        #4
        Re: Return True

        Rita,
        [color=blue]
        > If iCnt = 0 Then Return False[/color]

        How are you sure that this situation is not done consequently.

        Cor


        Comment

        • Olaf Baeyens

          #5
          Re: Return True

          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.


          Comment

          • RitaG

            #6
            Re: Return True

            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.

            "Joshua Flanagan" wrote:
            [color=blue]
            > 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
            >[/color]

            Comment

            • RitaG

              #7
              Re: Return True

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

              "Joshua Flanagan" wrote:
              [color=blue]
              > 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
              >[/color]

              Comment

              Working...