4D v14.3Writing a method |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v14.3
Writing a method
Writing a method
The 4D Method editor works much like a text editor. Writing a method is usually a combination of typing text, selecting components, and dragging items from the Explorer or other windows. You can also use various type-ahead functions to create methods faster. You can scroll through the contents of methods which can include up to 32,000 lines of code or 2 GB of text. The 4D Method editor provides basic syntax error-checking. Additional error-checking is performed when the method is executed. 4D uses standard text editing techniques for typing and editing in the Method editor. As you type, the characters appear at the location of the insertion point. You end each line by pressing the Return key or Enter key. Note: The Enter key on the numeric keypad behaves differently from the Enter key on the main keyboard. Use the Enter key on the numeric keypad to force 4D to check the syntax of the line of code without moving the insertion point to the next line. The Method editor uses display conventions (style, color) for the syntax elements. You can modify these conventions (see the Method editor). As you type, when you validate your entry, 4D evaluates the text of the line and applies the appropriate display format. 4D also indents each line to its proper level in relation to the preceding line when programming structures (for example If, End if) are used. You can use the arrow keys to move from line to line quickly. Using the arrow keys to move across several lines is quicker than clicking because the editor delays evaluating the line for errors. Under Windows, the code editor includes an Input Method Editor (IME) to facilitate code editing on Japanese or Chinese systems. The Method editor includes numerous navigation shortcuts. These shortcuts are listed in “Navigational keyboard shortcuts” section below. 4D allows you to use the drag-and-drop mechanism when writing methods. It is possible to drag and drop items from the Explorer, within the same method or between two methods.
You can now write a single statement on several lines by terminating each line of the statement with a backslash "\" character. 4D will consider all the lines at once. For example, both of the following statements are equivalent: You can automatically modify the case of selected characters using commands from the Case submenu in the Method menu or the context menu of the editor:
When you apply one of these commands to a text selection, the spaces and "_" characters are removed and the first letter of each word becomes uppercase. The Swap Expression function can be used to reverse the arguments of an expression assigning a value. For instance, variable1:=variable2 becomes variable2:=variable1 This function is extremely useful for reversing a set of assignments used to get or set properties, or to correct input errors. To use this function, select the line(s) to be modified, then choose the Swap Expression command in the Method menu, or in the context menu of the area. Within the selection, only the lines assigning a value will be modified. In addition to the standard copy-paste operation, 4D proposes two additional functions that let you work with the contents of different clipboards:
You can move the line where the cursor is directly without selecting it first using the Move Lines Up and Move Lines Down commands in the Method menu. You can also do this using the combination Alt/Option + Up Arrow or Down Arrow. Colored bars instantly show you where lines of code were modified since the method was opened: The change bars change colors to indicate whether or not the modifications were saved:
The Method editor has an autocomplete function. 4D automatically displays suggestions based on the first few characters typed. In the example given below, typing the string “cop” causes the display of a blue triangle indicating that several suggestions are available: Note: When the characters you enter correspond to a single possibility, this suggested value appears grayed out (and is inserted if you hit the Tab key): If there are several suggestions available, 4D displays them in a pop-up list when you hit the Tab key: The list is in alphabetical order. Choose the value by double-clicking it or scroll the list using the arrow keys of the keyboard and then hit Enter, Carriage Return or Tab to insert the selected value. By default, you can also insert a suggested value by hitting one of the following delimiter ( ; : = < [ { keys after selecting a value: the value inserted is then followed by the delimiter, ready for data entry. + ( key --> Note: You can disable the use of delimiters for inserting suggested values on the Methods Page of the User preferences. You can press the Esc key to close the pop-up list or you can continue typing while it is open. The values suggested in the pop-up list are updated as additional characters are typed. If the characters typed correspond to different types of objects, the list displays them in their current style. The following types of objects can be displayed:
Note: For practical reasons, you can disable the automatic display of the list of suggestions for constants, local and interprocess variables and/or tables. These options are found on the Methods Page of the User preferences. The Select Enclosing Block function is used to select the “enclosing block” of the code containing the insertion point. The enclosing block can be defined by:
If a block of text is already selected, the function selects the enclosing block of the next highest level and so on, until the entire method is selected. Pressing Ctrl+Shift+B (Windows) or Command+Shift+B (Mac OS) enables you to reverse this operation and deselect the last enclosing block selected. Note: If the insertion point is placed in an If or Else type structure, the enclosing block will be the one containing, respectively, the If or Else statement. You can double-click to select individual "words". When the item name referenced (command, constant, method, etc.) contains spaces, you can select the whole name (including spaces) by using the Alt/Option + Double-click combination. Standard keyboard shortcuts to navigate the code are available in 4D’s Method editor. Note: Under Mac OS, substitute the Command key for the Ctrl key (Windows).
4D automatically checks the method syntax to see if it is correct. If you enter text or select a component that is not syntactically correct, 4D displays a symbol to indicate the incorrect expression . When you move the mouse over the symbol, a help tip displays the cause of the error: When entering code, you can immediately check the syntax of the current line (without advancing to the next line) by pressing the Enter key on the numeric keypad. 4D evaluates the line, formats it, marks any errors, and places the insertion point at the end of the line. When a line of a method is marked as having improper syntax, check and fix the entry. If the line is now correct, 4D removes the error symbol. When you save or close the window, the entire method is validated. You can also force validation by pressing the Enter key. When the method is validated, 4D checks for basic syntax errors and for the structure of the statements (If, End if and so on). 4D also checks for matching enclosing characters in the code such as parentheses or quotation marks. When you type an enclosing character, 4D indicates the match by framing the start/end characters with gray rectangles: Note: If you click on an enclosing character in the code, 4D indicates its match with gray rectangles by default. You can modify the way 4D indicates matching enclosing characters or disable this feature by means of the "Matching parentheses" option on the Methods Page of the Preferences. The Method editor can only check for obvious syntax errors (misspellings and the like). It does not check for errors that only occur during execution. Execution errors are caught by 4D when the method is executed. 4D provides a debugger (see Debugging) for handling and correcting these errors. The compiler also provides indispensable help for detecting errors. For more information about the compiler, refer to the Compilation chapter. The Method editor provides various contextual information using help tips which appear when you move the mouse over an object.
You can display the HTML documentation of a 4D language command at any time: to do so, select the complete command name or simply place the cursor in the name and press F1. The complete documentation of the command is displayed in a new window of your default browser. 4D looks for the on-line documentation of the command (on the 4D Doc Center site) or searches locally depending on the settings made in the application Preferences (see Documentation location). Comments are inactive lines of code. These lines are not interpreted by the program (4D does not apply any particular style within comments) and are not executed when the method is called. To create a comment, you just need to insert the // characters at the beginning of a line. The length of comments is limited to the maximum size of a line, which is 32,000 characters. The Comment/Uncomment command, found in the Method menu as well as in the Method editor context menu, is used to mark a group of selected lines of code as comments, or, on the contrary, to remove the comment characters from a selection. --> When the selection contains only active code, the Comment command is applied. When the selection includes both active code and commented lines, an additional pair of comment characters ( // ) is added to the latter; this way, they will retain their initial commented status if the line is subsequently “uncommented.” When the selection contains only commented lines, the Uncomment command is applied. Note: The Comment/Uncomment command only operates with full lines — it cannot be used to comment only part of a line. The Method editor allows you to use escape sequences (also called escape characters). An escape sequence is a sequence of characters that can be used to replace a “special” character. The sequence consists of a backslash \, followed by a character. For instance, \t is an escape sequence for the Tab character. Escape sequences facilitate the entry of special characters: the previous example (\t) replaces the entry "Character(Tab)". In 4D, the following escape sequences can be used:
Note: It is possible to use either upper or lower case in escape sequences. In the following example, the Carriage return character (escape sequence \r) is inserted in a statement in order to obtain the dialog box shown: ALERT("The operation has been completed successfully.\rYou may now disconnect.") Warning: The \ (backslash) character is used as a separator in pathnames under Windows. In general, 4D will correctly interpret Windows pathnames entered in the Method editor by replacing the single backslash \ with a double backslash \\. For instance, C:\Folder will become C:\\Folder. However, if you write “C:\MyDocuments\New”, 4D will display “C:\\MyDocuments\New”. In this case, the second backslashþis interpreted incorrectly as \N (an existing escape sequence). You must therefore enter a double backslash \\ when you want to have a backslash in front of a character used in one of the escape sequences recognized by 4D. Several functions in the Method editor make code easier to read and facilitate browsing among the statements. 4D code located inside loops and conditions can now be collapsed or expanded, in order to facilitate the reading of methods:
If you place the mouse over the expand button [...], a help tip appears, displaying the first lines of the hidden code. A collapsed portion of code can be selected, copied, pasted or deleted. All the lines included therein will be copied, pasted or deleted respectively. When a portion of code is pasted, it is automatically expanded. There are several ways to expand and collapse code:
Two commands make it easier to move around within code structures (e.g. If...Else...End if):
These commands are found in the Method menu as well as the context menu of the editor. You can also use the following shortcuts:
4D lets you associate bookmarks with certain lines in your methods. You can then browse quickly within the code by passing from one bookmark to another using specific commands. A bookmark moves along with its original row if additional rows are inserted in the method. Bookmarks are saved with the methods. Bookmarks are managed using the Bookmarks submenu of the Method menu:
Note: You can use bookmarks as markers for lines that contain an item found by a search. In this case, 4D automatically adds the bookmarks. For more information, refer to Find and replace in methods. |
PROPERTIES
Product: 4D SEE ALSO TAGS IME ARTICLE USAGE
4D Design Reference ( 4D v14 R2) Inherited from : Writing a method ( 4D v12.4) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||