Skip to Content
avatar image
Former Member

Best Practice for Debugging Code

Hi!

I am new to development in Crystal. What method do you recommend to verify my formulas are doing what I expect? For example, in other environments I would print or prompt the result.

What is the best approach in Crystal?

Any help you can provide will be appreciated.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 13, 2010 at 06:32 PM

    I just tried Carl's suggestion of using the debug statement but must be doing something wrong because I am just getting a "division by zero" prompt as a result, no values.

    Any ideas what I forgot to do?

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      I'm sorry, I should have mentioned to put this at the BOTTOM of the formula, not at the top. The values displayed are what they are at the point of the divide by zero error. If the division is first thing in the formula, none of the other calculations have been performed yet... (Actually, you want to put it at the exact point where you want the breakpoint. That can be in the middle of the formula.)

      HTH,

      Carl

  • Jan 13, 2010 at 06:06 PM

    There is no single or best method. I will sometimes comment out portions of a formula to check results at a certain stage. If you are working with a variable shared across formulas or sub-reports, you might create a display formula to show the current value. I have also created copies of a formula enabling one piece of an expression at a time to make sure it evaluates as I expect it should.

    Fuskie

    Who thinks the best method is to have validated results to compare the report against...

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 13, 2010 at 06:09 PM

    It really depends on what you're trying to achieve. If it's simply seeing if the calculation is right, just drop the field in the appropriate format and have it printed (or add a new format for this purpose that you can suppress or display).

    If a formula comes up with a value and you can't figure out why, my approach has been to add the following code to the formula in question (basic syntax):

    dim debug as number
    debug = debug / debug
    

    This will cause a divide by zero error, which will then cause Crystal to pull up the formula. On the left of the window, all fields and variables used in the formula are displayed with their current value. (You need to use the "debug" variable because Crystal Designer will give a syntatical divide by zero error if you code "formula = 1 / 0"...)

    As written, the breakpoint will hit on the first iteration of the formula. You can cause the "breakpoint" to happen on later iterations by using something like:

    global dbgcnt as number
    dim debug as number
    dbgcnt = dbgcnt + 1
    if dbgcnt = 5 then
      debug = debug / debug
    end if
    

    where the "5" is the iteration number that you want it to break on.

    If anyone has a better way to "set a breakpoint", I'd love to hear it! (Ah ha! A great idea for the Suggestions thread. Going there now!)

    HTH,

    Carl

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2010 at 09:40 PM

    Hi Carl,

    I must still be doing something wrong as it still isn't working for me. Can I use your suggestion within a custom function?

    Below is my code:

    Function cdQSIPeriod () As String

    dim debug as number

    dim YY as number

    dim MM as number

    YY = Year(CurrentDate)

    MM = Month(CurrentDate)

    If MM = 1 then

    MM = 12

    YY = YY - 1

    else

    MM = MM - 1

    End If

    cdQSIPeriod = MM&YY

    debug = debug / debug

    End Function

    Any help you can provide will be appreciated.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2010 at 09:43 PM

    Never mind, Carl. I realize now I wasn't looking in the left pane. Your suggestion works.

    Thank you!

    Add comment
    10|10000 characters needed characters exceeded