4D v16

ポインター

ホーム

 
4D v16
ポインター

ポインター    


 

 

ビデオ中で説明した操作に加え、以下の点も留意してください:

ポインターの利用はとても簡単です。ポインターを使用すればプログラムをより汎用的および柔軟にできます。

ポインターのコンセプトは単純です。日常生活のたとえで見てみましょう。

“私の犬は家にいます”という文章では2つのポインターあるいは間接参照を使用しています。

実際“私の犬”というのは名前でも犬の品種でもありません。ここで必要なのは、私たちがいまどの犬について話をしているのかを知ることです。これは家についても同様です。

ポールが“それを引き出しにしまっておいて”といった場合、彼はどの引き出しのことを言っているのでしょう。

ポールは引き出しを指で指し示しているはずです。ポールが指差している状態を指示された人が見て、引き出しを知ることができます。これを参照の解決といいます。

この単純な記法を4Dでも使用します。引き出しや犬をフィールドや変数に置き換えるだけです。

4Dに"このテーブル"に対するクエリを行うよう指示するとき、ポインターを使用するためにはクエリ対象のテーブルのポインターを取得します:

 MyPointer:=->[TABLE]

もしポインターについてまだ混乱しているとしても気にしないでください。すぐに理解して、そのパワーに気づかれることと思います。
ポインターはしばしばメソッドの引数として使用されます。少し時間を使ってポインターについて調べることをお勧めします。例えば配列やテーブルなど一部のオブジェクトはポインターでしかメソッドの引数として渡すことができません。汎用的なメソッドを記述しようとすると、ポインターの利用は必須となります。

ポインターのリストが必要な場合はポインター型の配列を使用します。

オブジェクト名 (文字列) から変数のポインターを取得するにはGet pointerコマンドを使用します:

 Get pointer("MyVariable"+String($i))

In this video, we're going to learn about pointers and how to optimize their programming and make it generic.

As we saw in the previous exercise, setting options for each action (query, order by, etc.) in the NAVIGATION_FUNCTIONS  method can quickly become tedious. So, we need to set the action towards the table corresponding to the selected page.

In that case, it’s easier to indicate table to be used just once at the beginning of the method. We can store this directive in a variable and then use this variable in the commands.

Logically speaking, we can’t say that a variable is equal to a table; these objects are completely different and have completely different content as well. We’ve already learned that a variable can be an integer, number, text, picture, etc. It contains a value of the relevant type. A table isn’t “content” like a numeric value is.

The solution is to use a variable of the pointer type.

So we're going to set this variable, which is a local variable:

 C_POINTER($TablePointer)

This variable exists in memory. Now to assign a value, we'll use the following type of syntax:

 $TablePointer:=->[INTERVENTIONS]

So as we saw for variables, we have:

  • initialization
  • assignment of value

and, later on, wherever the Interventions table should have been used, we'll be able to replace it with this variable, that we'll need to "de-point" in this case.

The advantage being that all the code is here, a simple copy-paste with only a change of table, can be replaced by these 2 same lines of code, provided that we have previously set the table to where it must point depending on the page number.

So we have to get the code that we wrote earlier and here we indicate that:

  • If we are on page 1, we point to the interventions (->[INTERVENTIONS])
  • If we are on page 2, we point to the technicians (->[TECHNICIANS])

That said, we no longer need to test the pages here and so it's no longer useful to have this duplicate program.

We have a 1st program here that lets us:

  • set the table concerned depending on the page
  • and then set the action to perform regardless of the page since the correct table was set.

The same principle applies for searches:

  • we'll move the code here
  • completely remove the "Case of" that is here
  • and replace "intervention" with a pointer to the table since we are on page 2

When we get here, the pointer will be a pointer to "Technicians" and so we will search in the "Technicians" table.

The number of records found is the number of records found in the "Technicians" table.

 
 

 
プロパティ 

プロダクト: 4D
テーマ: ポインター

 
履歴 

 
ARTICLE USAGE

セルフトレーニング ( 4D v16)