Skip to main content

Storage Method

Understanding how to use globals to store data is so important that we're now going to write the Store() method for ObjectScript.DataEntry4 "together," before you get to the next exercise. After the user correctly responds to the prompts and reviews the data, this code will give the user the option to store the data. As you'll see from the next few pages, you'll store the data in a global named ^PersonD.

For each person, you'll need a unique ID number, to be used as the subscript of ^PersonD, so that you can store multiple records. The very first time you run the code in ObjectScript.DataEntry4, ^PersonD doesn't exist. Therefore, you need to make sure that the first ID is 1, which you'll do easily using $Increment(). This will make ^PersonD into an array similar in conceptual structure to “normal” arrays (although it's still really an ordered tree): a database of people.

Each person's record is a three-item list of their data. Another common alternative would be using a delimiter character (for example, "^" or "*") between each answer so that the record looked like name^phone^intdob or name*phone*intdob. In this case, it's important to pick a delimiter that doesn't appear in the data; "," or "-" would be bad choices because those characters appear in the name and phone number. Using $ListBuild eliminates this issue.

Here is the first section of Store():

VS Code - ObjectScript

Class ObjectScript.DataEntry4
{

/// store the data
ClassMethod Store(answers as %String)
{
    read !, "Store? (y/n): ", yn  // ask if user wants to store
    // only go on if user says yes
    if ((yn '= "y") && (yn '= "Y")) {
        write "...not stored."
        quit
    }

    set id = $increment(^PersonD)  // use $increment to generate a new ID
    set ^PersonD(id) = answers  // store the answers
   
    // ...remainder of this method described and completed on next page
    
}
}
Copy code to clipboard
FeedbackOpens in a new window