メインコンテンツまでスキップ
バージョン: 20 R7 BETA

This

This : Object

引数説明
戻り値ObjectCurrent element or object

説明

The This command returns a reference to the currently processed object.

This の値は、呼ばれ方によって決まります。 This の値は実行時に代入により設定することはできません。また、呼び出されるたびに違う値となりえます。

This command can be used in different contexts, described below. Within these contexts, you will access object/collection element properties or entity attributes through This.<propertyPath>. For example, This.name or This.employer.lastName are valid pathes to object, element or entity properties.

In any other context, the command returns Null.

クラス関数

クラスコンストラクター 関数が new() 関数により使用された場合、その内部の This はインスタンス化される新規オブジェクトを指します。

// クラス: ob

Class Constructor

// This のプロパティを
// 代入によって作成します

This.a:=42
// 4Dメソッドにて 
$o:=cs.ob.new()
$val:=$o.a //42

コンストラクター内で Super キーワードを使ってスーパークラスのコンストラクターを呼び出す場合、必ず This より先にスーパークラスのコンストラクターを呼ぶ必要があることに留意してください。順番を違えるとエラーが生成されます。 See this example.

基本的に、This はメソッドの呼び出し元のオブジェクトを指します。

//Class: ob

Function f() : Integer
return This.a+This.b

この場合、プロジェクトメソッド内には次のように書けます:

$o:=cs.ob.new()
$o.a:=5
$o.b:=3
$val:=$o.f() //8

この例では、変数 $o に代入されたオブジェクトは f プロパティを持たないため、これをクラスより継承します。 f は $o のメソッドとして呼び出されるため、メソッド内の This は $o を指します。

フォーミュラオブジェクト

In the context of the execution of a formula object created by the Formula or Formula from string commands, This returns a reference to the object currently processed by the formula.

For example, you want to use a project method as a formula encapsulated in an object:

 var $person : Object := New object
$person.firstName:="John"
$person.lastName:="Smith"
$person.greeting:=Formula(Greeting)
$g:=$person.greeting("hello") // returns "hello John Smith"
$g:=$person.greeting("hi") // returns "hi John Smith"

With the Greeting project method:

 #DECLARE($greeting : Text) : Text
return $greeting+" "+This.firstName+" "+This.lastName

リストボックス

In the context of a list box associated to a collection or an entity selection, during the On Display Detail or the On Data Change events, This returns a reference to the collection element or entity accessed by the list box to display the current row.

If you use a collection of scalar values in a list box, 4D creates an object for each element with a single value property. Thus, the element value is returned by the This.value non-assignable expression.

例題 1

A collection of objects, each with this structure:

{  
"ID": 1234
"name": "Xavier",
"revenues": 47300,
"employees": [
"Allan",
"Bob",
"Charlie"
]
},{
"ID": 2563
"name": "Carla",
"revenues": 55000,
"isFemale": true
"employees": [
"Igor",
"Jane"
]
},...

In the list box, each column refers to one of the properties of the object, either directly (This.name), indirectly (This.employees.length), or through an expression (getPicture) in which can be used directly. The list box looks like:

The GetPicture project method is automatically executed during the On display detail event:

  //GetPicture Method
#DECLARE -> $genderPict : Picture
If(This.isFemale)
$genderPict:=Form.genericFemaleImage
Else
$genderPict:=Form.genericMaleImage
End if

Once the form is executed, you can see the result:

例題 2

You want to display entities from the following structure in a list box:

You build a list box of the "Collection or entity selection" type with the following definition:

注:

  • This.ID, This.Title and This.Date directly refers to the corresponding attributes in the ds.Event dataclass.
  • This.meetings is a related attribute (based upon the One To Many relation name) that returns an entity selection of the ds.Meeting dataclass.
  • Form.eventList is the entity selection that is attached to the list box. The initialization code can be put in the on load form event:
 Case of  
:(Form event code=On Load)
Form.eventList:=ds.Event.all() //returns an entity selection with all entities
End case

Once the form is executed, the list box is automatically filled with the entity selection:

参照

Self
Super