Strange error - help?

Discussion in 'Questions (Windows Mobile)' started by dlfallen, Jun 5, 2009.

  1. dlfallen

    dlfallen Active Member Licensed User

    I am trying to convert some old code into Basic4ppc (I have working versions in QuickBasic and VBdos). The program deals with the Standard Normal Distribution and will compute the area corresponding to a Z score, or a Z score corresponding to an area.

    After much tweaking of the code, I am almost there. Going from Z score to area works fine. But going from area to Z score generates a strange error message. The really strange part is the point in the program at which the error message occurs. Here is the relevant section of code (the full program is attached):

    35 Private Sub CalcZ()
    36 Form1.Text = "Calculate Z for Given Area"
    38 target = AreaText.Text
    39 If target = 0 Then Return
    41 If Option1.Value = True Then
    42 target = target * 2
    43 t$ = "Z (one-tailed) = "
    44 Else
    45 t$ = "Z (two-tailed) = "
    46 End If
    48 z = 0
    49 OldTR = 0
    51 For i = 1 To 10
    52 z = z + 1 * 10 ^ (-i)
    55 calcit:
    57 tr = gammq(0.5, (z ^ 2) / 2)
    59 ' prog hits infinate Loop when tr > target
    60 ' AND tr-target less than machine precision.
    61 ' This If block takes care of it.
    63 If OldTR = tr Then
    64 OldTR = 0
    65 tr = target
    66 Else: OldTR = tr
    67 End If
    69 If tr > target Then
    70 z = z + 1 * 10 ^ (-i)
    71 Goto calcit
    72 End If
    73 z = z - 1 * 10 ^ (-i)
    74 Next i
    76 ResultsText.Text = t$ + Format(z, "N8")
    78 End Sub
    80 Private Sub Command1_Click()
    81 PrecLev = 12
    82 PrecLev = PrecLev + 1
    84 CalcArea
    86 End Sub
    88 Private Sub Command2_Click()
    89 PrecLev = 12
    90 PrecLev = PrecLev + 1
    92 CalcZ
    93 End Sub

    The subroutine CalcZ (starting at line 35) is called at line 92. I put a breakpoint on line 74 and checked the values at that point. Everything is correct, Z is 1.9 at that point and i = 1. When I press F8 what should happen is i increments to 2, control is passed to line 51, and the next significant digit of Z should be computed. Instead, when I press F8 I get the following error message:
    An error occured on sub main.command2_click.

    Line number 92

    Error description:
    Length cannot be less than zero.
    Parameter name: length

    I don't understand how executing line 74 can cause an error on line 92, much less understand what the error is.

    Can anyone help?
  2. klaus

    klaus Expert Licensed User

    Hi dlfallen,

    Here you are, there were several problems:

    - replaced the Goto by a Do-Loop Until loop

    - replaced ResultsText.Text = t$ + Format(z, "N8")
    by ResultsText.Text = t & Format(z, "N8")

    - replaced If Option1.Value = True Then
    by If Option1.Checked = True Then

    Best regards.
    Last edited: Jun 5, 2009
  3. dlfallen

    dlfallen Active Member Licensed User

    Thanks Klaus,

    The "t$ +" and the "option1.value" errors were, of course, errors of translation. The key to making the program run was the replacing the label/GoTo logic with Do/Loop until. This would be good programming technique even if I had not been getting an error message.

    Although it is wonderful that the program now works, I really would like to understand why I was getting an error with the original code. The GoTo statement is supported by Basic4ppc. According to the documentation, I should not have any difficulties unless I was transferring control out of the sub. Oh well, if I get a similar error on other projects I can alway try replacing GoTo statments with Do loops.

    Thanks again Klaus, it was great of you to take the time to help!
  4. klaus

    klaus Expert Licensed User

    Thanks Klaus,

    Even though Goto is supported by Basic4PC, but Basic4PC is much less permissive with Goto's than older Basic languages like QuickBasic and also Visual Basic.
    I had also some trouble with Goto's when transferring older programs to Basic4PPC.

    Best regards.
  5. agraham

    agraham Expert Licensed User

    Actually I would have expected the code with the Goto to work, as indeed it does if you optimise compile it with the other fixes in place, as the scoping looks correct. You cannot use Goto to jump into If .. End If blocks or loops but you should be able to jump out of and within them which is what that code does.

    I wonder if Goto got broken in a recent Basic4ppc version but no-one has picked it up as it is hardly used?
  6. dlfallen

    dlfallen Active Member Licensed User

    Yes, it seems it should work, but it doesn't. Fortunately, GoTo statements are rarely, if ever, necessary so it's not a big issue. There were many changes I had to make to the old DOS Basic code to get it to work in Basic4ppc. I kept a list of all the types of changes I had to make which will make it easier to translate other antique code in the future. Getting rid of GoTo statements is just one more item on the list.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice