GeersJ
GeersJ

Reputation: 115

Return Default Value When No Match Found

I have a function GetQuantity that returns a decimal. In some cases I want to return nothing i.e. and empty string so that ' ' is displayed.

Actual Behavior:

  1. GetQuantity(1) -> 1.0
  2. GetQuantity(2) -> 2.0
  3. GetQuantity(3) -> 3.3

Desired Behavior:

  1. GetQuantity(1) -> 1.0
  2. GetQuantity(2) -> 2.0
  3. GetQuantity(3) -> ' '

In case 3 I can obviously return -1.0 or something but that is not what I need.

FUNCTION GetQuantity RETURNS DECIMAL(INPUT num  AS INTEGER):
    DEFINE VARIABLE quantity AS DECIMAL NO-UNDO FORMAT "->,>>>,>>9.9<<<<<<<<".

    quantity = 3.3. //initialization is neccessary as IRL my value is initialized

    IF num = 1 THEN DO:
        RETURN 1.0.
    END.

    ELSE IF num = 2 THEN DO:
        RETURN 2.0.
    END.

    RETURN quantity. //base case return ' '
END.

DISPLAY GetQuantity(3)

Upvotes: 1

Views: 143

Answers (2)

jdpjamesp
jdpjamesp

Reputation: 772

An alternative solution to Mike's above would be to change getQuantity() to return a Character value instead of the decimal it currently does. Although of course this could cause a lot of work fixing all the calling points. I don't know the extent of its use.

Also, if you're in a position where the number of conditions is greater than you've shown, a CASE would be far easier to maintain than lots of IF statements. Something like this:

FUNCTION GetQuantity RETURNS CHARACTER(INPUT num  AS INTEGER):
    DEFINE VARIABLE quantity AS CHARACTER NO-UNDO INITIAL "3.3".

    CASE num:
        WHEN 1 THEN quantity = "1.0".
        WHEN 2 THEN quantity = "2.0".
        OTHERWISE quantity = "". 
    END CASE.

    RETURN STRING(quantity). 
END.

DISPLAY GetQuantity(3).

Upvotes: 3

Mike Fechner
Mike Fechner

Reputation: 7192

One way would be to return ? in case of the default and handle this in your output routine.

As ? is also the result of a failing calculation (div by 0) this might be dangerous.

The alternative would be to write a class with a Value property and a Default flag and an ToString() override.

BLOCK-LEVEL ON ERROR UNDO, THROW.

USING Progress.Lang.*.

CLASS Test.SampleValueHolder: 

    DEFINE PUBLIC PROPERTY Value AS DECIMAL NO-UNDO
    GET.
    PRIVATE SET.

    DEFINE PUBLIC PROPERTY IsDefault AS LOGICAL NO-UNDO
    GET.
    PRIVATE SET.

    CONSTRUCTOR PUBLIC SampleValueHolder (pdeValue AS DECIMAL, 
                                          plDefault AS LOGICAL):

        ASSIGN THIS-OBJECT:Value     = pdeValue 
               THIS-OBJECT:IsDefault = plDefault .                                          

    END CONSTRUCTOR.

    METHOD PUBLIC OVERRIDE CHARACTER ToString ():

        IF THIS-OBJECT:IsDefault THEN 
            RETURN "" .
        ELSE 
            RETURN STRING (THIS-OBJECT:Value, "->,>>>,>>9.9<<<<<<<<") .

    END METHOD.

END CLASS.

Now you can either RETURN NEW SampleValueHolder (1.0, FALSE) or RETURN NEW SampleValueHolder (?, TRUE) into a Variable of Type Test.SampleValueHolder.

And whenever you display STRING () of that value, you either get the formatted value or "" when it's the default.

Upvotes: 5

Related Questions