Managing Methods and Classes
プロジェクトで使用される 4D コードは、 メソッド および クラス に記述されます。
4D IDE には、コードを作成・編集・エクスポート・削除するためのさまざまな機能があります。 通常は、4D付属 の コードエディター を使用して、コードを管理します。 他にもVS Code などのエディターを使用することもでき、VS Code に対しては4D-Analyzer 拡張機能 がご利用いただけます。
メソッドの作成
4D のメソッドは、/Project/Sources/ フォルダーの適切なフォルダー内の .4dm ファイルに格納されます。
いくつかの種類のメソッド を作成することができます:
- すべてのメソッドは、エクスプローラー ウィンドウから作成または開くことができます (フォームエディター から管理されるオブジェクトメソッドを除く)。
- プロジェクトメソッドは、ファイル メニューやツールバー (新規/メソッド... または 開く/メソッド...)、コードエディターウィンドウ のショートカットを使っても作成したり開いたりできます。
- Triggers can also be created or opened from the Structure editor.
- フォームメソッドは、フォームエディター からも作成したり開いたりできます。
クラスの作成
User classes
4D においてユーザークラスとは、/Project/Sources/Classes/ フォルダーに保存された専用のメソッドファイル (.4dm) によって定義されます。 ファイル名がクラス名になります。 For example, a class named "Polygon" will be stored in the following file:
Project フォルダー Project Sources Classes Polygon.4dm
You can create a class file from the File menu or toolbar (New > Class...) or in the Methods page of the Explorer window. Ctrl+Shift+Alt+k ショートカットも使用できます。
エクスプローラーの メソッド ページにおいて、クラスは クラス カテゴリに分類されています。
クラスを新規作成するには次の方法があります:
- クラス カテゴリを選択し、
ボタンをクリックします。
- エクスプローラーウィンドウの下部にあるアクションメニュー、またはクラスグループのコンテキストメニューから 新規クラス... を選択します。
- エクスプローラーのホームページのコンテキストメニューより 新規 > クラス... を選択します。
クラスを命名する際には、次のルールに留意してください:
- A class name must be compliant with property naming rules.
- クラス名の大文字・小文字は区別されます。
- 競合防止のため、データベースのテーブルと同じ名前のクラスを作成するのは推奨されないこと
ORDA classes
ORDA data model user classes are high-level class functions created above the data model.
An ORDA data model class is defined by adding, at the same location as regular class files (i.e. in the /Sources/Classes folder of the project folder), a .4dm file with the name of the class. たとえば、Utilities データクラスのエンティティクラスは、UtilitiesEntity.4dm ファイルによって定義されます。
各データモデルオブジェクトに関わるクラスは、4D によってあらかじめ自動的にメモリ内に作成されます。

空の ORDA クラスは、デフォルトではエクスプローラーに表示されません。 表示するにはエクスプローラーのオプションメニューより データクラスを全て表示 を選択します:
ORDA ユーザークラスは通常のクラスとは異なるアイコンで表されます。 空のクラスは薄く表示されます:

ORDA クラスファイルを作成するには、エクスプローラーで任意のクラスをダブルクリックします。 4D creates the class file and add the extends code. たとえば、Entity クラスを継承するクラスの場合は:
Class extends Entity
定義されたクラスはエクスプローラー内で濃く表示されます。
定義された ORDA クラスファイルを 4Dコードエディターで開くには、ORDA クラス名を選択してエクスプローラーのオブションメニュー、またはコンテキストメニューの 編集... を使用するか、ORDA クラス名をダブルクリックします:
ローカルデータストア (ds) に基づいた ORDA クラスの場合には、4D ストラクチャーウィンドウからも直接クラスコードにアクセスできます:

4D IDE (統合開発環境) におけるサポート
各種 4Dウィンドウ (コードエディター、コンパイラー、デバッガー、ランタイムエクスプローラー) において、クラスコードは "特殊なプロジェクトメソッド" のように扱われます:
- コードエディター:
- クラスは実行できません
- クラスメソッドはコードのブロックです
- オブジェクトメンバーに対する 定義に移動 操作はクラスの Function 宣言を探します。例: "$o.f()" の場合、"Function f" を見つけます。
- クラスのメソッド宣言に対する 参照箇所を検索 操作は、そのメソッドがオブジェクトメンバーとして使われている箇所を探します。例: "Function f" の場合 "$o.f()" を見つけます。
- variables typed as a user or ORDA class automatically benefit from autocompletion features. Entity クラス変数の例です:

- ランタイムエクスプローラーおよびデバッガーにおいて、クラスメソッドは
<ClassName>コンストラクターまたは<ClassName>.<FunctionName>形式で表示されます。
メソッドやクラスの削除
既存のメソッドやクラスを削除するには:
- ディスク上で "Sources" フォルダーより .4dm ファイルを削除します。
- 4D エクスプローラーでは、メソッドやクラスを選択した状態で
をクリックするか、コンテキストメニューより 移動 > ゴミ箱 を選択します。
オブジェクトメソッドを削除するには、フォームエディター で、オブジェクト メニューから オブジェクトメソッド消去 を選択します。
Design Object Access commands
You can access the contents and paths of all methods in your applications by programming, thanks to the "Design Object Access" command theme. This source toolkit facilitates the integration into your applications of code control tools and more particularly version control systems (VCS). It also lets you implement advanced systems for code documentation, for building a custom explorer or for organizing scheduled backups of the code saved as disk files.
The following principles are implemented:
- Each method and form in a 4D application has its own address in the form of a pathname. For example, the trigger method for table 1 can be found at "[trigger]/table_1". Each object pathname is unique in an application.
- You can access objects in the 4D application using the commands of the "Design Object Access" command theme, for example
METHOD GET NAMESorMETHOD GET PATHS. - Most of the commands in this theme work in both interpreted and compiled mode. However, commands that modify properties or access contents executable from methods can only be used in interpreted mode (see the table below).
- You can use all the commands of this theme with 4D in local or remote mode. However, keep in mind that you cannot use certain commands in compiled mode: the purpose of this theme is to create custom development support tools. You must not use these commands to dynamically change the functioning of a database that is running. For example, you cannot use
METHOD SET ATTRIBUTEto change a method attribute according to the status of the current user. - When a command of this theme is called from a component, by default it accesses the component objects. In this case, to access objects of the host, you just pass a
*as the last parameter.
Use in compiled mode
For reasons related to the principle of the compilation process, only certain commands in this theme can be used in compiled mode. The following table indicates the available of the commands in compiled mode:
The error -9762 "The command cannot be executed in a compiled database." is generated when the command is executed in compiled mode.
Creation of pathnames
Pathnames generated for 4D objects must be compatible with the file management of the operating system. Characters that are forbidden at the OS level such as ":" are automatically encoded in method names, so that generated files may be integrated automatically in a version control system.
Here are the encoded characters:
| 文字 | Encoding |
|---|---|
| " | %22 |
| * | %2A |
| / | %2F |
| : | %3A |
| < | %3C |
| > | %3E |
| ? | %3F |
| \ | |
| \ | %5C |
| % | %25 |
例題
Form?1 is encoded Form%3F1
Button/1 is encoded Button%2F1