メインコンテンツまでスキップ
バージョン: 次へ

メソッド

メソッドとは、1つ以上の動作を実行するコードのことです。 メソッドは、1つ以上のステートメントで構成されます。

ステートメントは 1つの命令を実行し、単純な場合もあれば複雑な場合もあります。 ステートメントは 1つの命令を実行し、単純な場合もあれば複雑な場合もあります。 ステートメントとは通常、メソッドの 1行のことを指します (必要に応じて \ 文字を使って行を分割 することができます。

メソッドは最大 2GBのテキストまたは、32000行まで記述できます。

メソッドタイプ

4D ランゲージにおいて、数種類のメソッドが存在します。 その呼び出し方によって、メソッドは区別されます:

自動呼び出しのコンテキスト引数の受け取り説明
プロジェクトメソッドOn demand, when the project method name is called任意のアクションを実行するためのコードです。 作成されたプロジェクトメソッドは、そのプロジェクトのランゲージの一部となります。
オブジェクト (ウィジェット) メソッドメソッドが設定されたフォームオブジェクトに関連したイベント発生時に×フォームオブジェクト (ウィジェットとも呼びます) のプロパティです。
フォームメソッドメソッドが設定されたフォームに関連したイベント発生時に×フォームのプロパティです。 フォームメソッドを使用してデータとオブジェクトを管理することができます。 ただし、これら目的には、オブジェクトメソッドを使用する方が通常は簡単であり、より効果的です。
トリガー (別名 テーブルメソッド)テーブルのレコード操作 (追加・削除・修正) の度に×テーブルのプロパティです。 トリガーは、データベースのレコードに対して「不正な」操作がおこなわれることを防ぎます。
データベースメソッド作業セッションのイベント発生時に○ (既定)4D には 16のデータベースメソッドがあります。
クラスクラスのオブジェクトがインスタンス化されたとき、あるいは他のメソッドやデータベースフィールド 内においてオブジェクトインスタンス上でクラスの関数が実行されたときに自動的に呼び出されます。◯(クラス関数)オブジェクトのクラスのconstructor, properties関数 を宣言および設定するためには、Class が使用されます。 クラス 参照。 See Classes and Function class. See Classes and Function class.

Language tokens

4D's language includes a unique tokenization system for constants, commands, tables, fields and keywords names that are used in the code. Tokenizing these names means that as you type in the code editor they are stored internally as absolute references (numbers) and then restored as text during execution or display depending on the context. This allows you to guarantee that the code will always be interpreted correctly, even if you rename your tables or fields, or when 4D language commands are renamed over the course of different application versions.

Note: This also ensures automatic translation of the code when you have enabled the "Use regional system settings" preference and open your databases with 4D versions in different languages.

Tokenisation is completely transparent for 4D developers when working in the 4D code editor, and you generally won't need to worry about it. However, there are two cases where you might need to take action regarding tokenization: if you want to disable it, and if you want to use tokenization in your formulas.

Disabling tokenization

When your project is stored on a version control system (VCS) such as GitHub or GitLab, you may want to disable tokenization to make the code more readable on the external platform. To do this, you can deselect the Include tokens in project source files preference to prevent tokens from being stored in your new projects.

You can configure your existing projects to save code without tokens by inserting the following key in the <applicationName>.4DProject file using a text editor:

"tokenizedText": false

この設定は、メソッドが保存されるときにのみ考慮されます。 つまり、再保存しない限り、プロジェクト内の既存メソッドはそのまま残されます。

Using tokens in formulas

A text-based 4D formula is a text that is interpreted at runtime, and not as it is typed. In fact, this is the case as soon as 4D code is expressed as raw text, more specifically when code is exported and then imported using the METHOD GET CODE and METHOD SET CODE commands, copied/pasted or interpreted from 4D HTML tags. To benefit from tokenization mechanisms in these contexts, you just need to use an explicit syntax which consists in preceding object names in the language by their token.

Token syntax

For tokenizable named elements contained in expressions, 4D offers a special syntax you can use to reference the tokens directly: you just need to add a specific suffix after the element name to indicate its type (command, field, etc.), followed by its reference. The token syntax is detailed in this table:

機能Example (standard syntax)SuffixExample (token syntax)コメント
4D CommandString(a):CxxString:C10(a)xx is the command number
4D ConstantPi:Kxx:yyPi:K30:1xx is the ID of the constant group and yy is its index (position) within this group
Table[Employees]:xx[Employees:1]xx is the table number
フィールド[Employees]Name:xx[Employees:1]Name:2xx is the field number
4D PluginPV PRINT(area):Pxx:yyPV PRINT:P13000:229(area)xx is the plug-in ID and yy is the index of the command

Note: Uppercase letters (C, P) must be used in the suffixes; otherwise, they will not be interpreted correctly.

When you use this syntax, you guarantee that your formulas will be interpreted correctly even in the case of renaming or when the database is executed in a different language.

This syntax is accepted in all 4D formulas (or 4D expressions) regardless of the calling context:

Where to find the element numbers?

The token syntax requires the addition of the reference numbers of various elements. The location of these references depends on the type of element.

  • 4D commands: Command numbers can be found in the documentation ("Properties" area) as well as on the Commands page of the Explorer.
  • Tables and fields: Table and field numbers can be obtained using the Table and Field commands. They are also displayed in the Inspector palette of the Structure editor.