User Defined Conversions

Published ByDateVersionKnowledge LevelKeywords
Sprezzatura Ltd01 MAY 19912.03+EXPERTCONVERSION, OCONV, ICONV

One of the most common forms of user defined conversion is that which takes a code and using it extracts a description from a code table for display. For example

SUBROUTINE CODE_LOOKUP(TYPE, PASSED, BRANCH, RETURNED)
  BEGIN CASE
    CASE TYPE = "ICONV"
     *   Assuming codes are MVs in field 5 and descs in 6
     CODES = XLATE("CODES", BRANCH[-1,"B*"],5,"X")
     LOCATE PASSED IN CODES USING @VM SETTING POS THEN
       STATUS() = 0
     END ELSE
       STATUS() = 1 ; PASSED = ""
     END
     RETURNED = PASSED
    CASE TYPE = "OCONV"
     CODES = XLATE("CODES", BRANCH[-1,"B*"],"","X")
     LOCATE PASSED IN CODES<5> USING @VM SETTING POS THEN
       RETURNED = CODES<6,POS>
     END ELSE
       RETURNED = PASSED
     END
  END CASE
RETURN

Note that in this example the DATA logic is kept separate from the APPLICATION logic (the ICONV itself does not suggest replacements, it flags that an error has occurred and lets the application logic suggest alternatives. This leads to more portable code (see future articles on system design)).

However when the user presses Ctrl-F10 and then F2 the uncoded version shows up. If the user then chooses the uncoded version, the OCONV logic is invoked and it redisplays as the coded version. If the user then selects this version and F9s it, the system attempts to re-ICONV the OCONVed value - even though the internal form is available - fails to ICONV it and thus finds no hits. To fix this problem amend the code as follows :-

  CODES = XLATE("CODES", BRANCH[-1,"B*"], "", "X")
  LOCATE PASSED IN CODES<5> USING @VM SETTING POS THEN
    RETURNED = PASSED
  END ELSE
    LOCATE PASSED IN CODES<6> USING @VM SETTING POS THEN
      RETURNED = CODES<5,POS>
    END ELSE
      STATUS() = 1 ; RETURNED = ""
    END
  END

(Volume 3, Issue 1, Page 7)