Skip to main content
Version: 20 R7 BETA

METHOD GET CODE

METHOD GET CODE ( path ; code {; option} {; *} )

ParameterTypeDescription
pathText, Text arrayText or Text array containing one or more method path(s)
codeText, Text arrayCode of designated method(s)
optionInteger0 or omitted = simple export (without tokens), 1 = export with tokens
*OperatorIf passed = command applies to host database when executed from a component (parameter ignored outside of this context)

This command is not thread-safe, it cannot be used in preemptive code.

Description

The METHOD GET CODE command returns, in the code parameter, the contents of the method(s) designated by the path parameter. This command can return the code of all types of methods: database methods, class definitions, triggers, project methods, form methods and object methods.

You can use two types of syntaxes, based either on text arrays, or text variables:

 var tVpath : Text // text variables
 var tVcode : Text
 METHOD GET CODE(tVpath;tVcode) // code of a single method
 ARRAY TEXT(arrPaths;0) // text arrays
 ARRAY TEXT(arrCodes;0)
 METHOD GET CODE(arrPaths;arrCodes) // code of several methods

You cannot mix the two syntaxes.

If you pass an invalid pathname, the code parameter is left empty and an error is generated.

In the text of the code generated by this command:

  • Command names are written in English for all versions of 4D, except when you use a French version and check the "Use regional system settings" preference (see Is a list). When you use the option parameter, the code can contain language tokens in order to make it independent from the 4D programming language and version (see below).
  • To increase code readability, text is indented with tab characters based on programming structures, like in the Method editor.
  • A line is added in the header of the code generated containing metadata used when importing code, for example:
  // %attributes = {"lang":"en","invisible":true,"folder":"Web3"}  

During an import, this line is not imported, it is only used to set the corresponding attributes (attributes that are not specified are reset to their default value). The "lang" attribute sets the export language and prevents an import into an application in a different language (in this case, an error is generated). The "folder" attribute contains the name of the method’s parent folder; it is not shown when the method does not have a parent folder.
Additional attributes can be defined. For more information, refer to the description of the METHOD SET ATTRIBUTES command.

The option parameter allows you to select the code export mode with respect to the tokenized language elements of the method(s):

  • If you pass 0 or omit the option parameter, the method code is exported without tokens, i.e. just like it is displayed in the Method editor.
  • If you pass 1 or the Code with tokens constant, the method code is exported with tokens, i.e. tokenized elements are followed by their internal reference in the code exported contents. For example, the expression "String(a)" is exported "String:C10(a)", where "C10" is the internal reference of the String command.

Tokenized language elements include:

  • 4D commands and constants,
  • Table and field names,
  • 4D plug-in commands.

Code exported with tokens is independent from any subsequent renaming of language elements. Thanks to tokens, code provided as text will always be interpreted correctly by 4D, whether by means of the METHOD SET CODE command or even by copy/paste. For more information about the syntax of 4D tokens, please refer to Using tokens in formulas.

If the command is executed from a component, it applies by default to the component methods. If you pass the * parameter, it accesses the methods of the host database.

Example 1

Refer to the example of the METHOD SET CODE command.

Example 2

This example illustrates the effect of the option parameter.

You want to export the following "simple_init" method:

 Case of
    :(Form event code=On Load)
       ALL RECORDS([Customer])
 End case

If you execute the following code:

 var $path : Text
 var $contents : Text
 $path:=METHOD Get path(Path project method;"simple_init")
 METHOD GET CODE($path;$contents;0) //no tokens
 TEXT TO DOCUMENT("simple_init.txt";$contents)

The resulting document will contain:

  //%attributes = {"lang":"en"} comment added and reserved by 4DCase of    : (Form event code=On Load)        ALL RECORDS([Customer])End case

If you execute the following code:

 var $path : Text
 var $contents : Text
 $path:=METHOD Get path(Path project method;"simple_init")
 METHOD GET CODE($path;$contents;Code with tokens) //use tokens
 TEXT TO DOCUMENT("simple_init.txt";$contents)

The resulting document will contain:

  //%attributes = {"lang":"en"} comment added and reserved by 4DCase of    : (Form event code:C388=On Load:K2:1)        ALL RECORDS:C47([Customer:1])End case

See also

METHOD SET CODE
Using tokens in formulas