KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to Improve Popup Menu Code Readability
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: November 4, 2024

When developing popup menus, it can be easy to develop code where menu options are referred to by their index:

$menuItems:="rock; paper; scissors"

$userChoice:=Pop up menu($menuItems)

If ($userChoice>0)
   Case of
      : ($userChoice=1)
        ALERT("rock")
      : ($userChoice=2)
        ALERT("paper")
      : ($userChoice=3)
        ALERT("scissors")
   End case
End if


This code is difficult to read and maintain. Especially if the menu is large and complex or needs to be updated with additional options, each of the indexes will need to be managed carefully. A better way to develop menu code is to have a collection that stores the names of each of the menu items like this:

$options:=New collection("rock"; "paper"; "scissors")

$userChoice:=Pop up menu($options.join(";"))

If ($userChoice>0)
   Case of
      : ($options[$userChoice-1]="rock")
        ALERT("rock")
      : ($options[$userChoice-1]="paper")
        ALERT("paper")
      : ($options[$userChoice-1]="scissors")
        ALERT("scissors")
   End case
End if



This way menu items can be referred to by their names rather than their index.