4D v16

汎用プログラミング (ポインター不使用)

ホーム

 
4D v16
汎用プログラミング (ポインター不使用)

汎用プログラミング (ポインター不使用)    


 

 


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

例題では、特定のことを達成するためのひとつの方法を、あくまでも一例としてお見せしています。

開発者自身の慣習や会社の標準手法により、異なる方法、フィールドや変数の命名規則、引数の順番などは変わってくるでしょう。
他の方法の方が適していると思われるならそれを試し、それぞれのメリットやデメリットを比較してみるとよいでしょう。そうして自身のスタイルが出来上がっていくことになります。

ひとつのメソッドは32個までの引数 ($1 から $32) を受け取ることができます。しかし7個以上の引数を渡すことは通常ありません。

汎用プログラミングについては是非慣れ親しんでください。

次の節ではここで説明したことに加え、ポインターの利用方法について説明します。

特定の機能を汎用メソッドという形で独立化すれば、その機能を利用する各メソッドが短くなって、コードは見通しが良く、メンテナンスもしやすくなります。
汎用プログラミングを適切に行うためには、いくつかの注意点があります:

  • 同じ内容のコードを複数回記述するようなことがあれば、おそらくその部分を汎用メソッドとして実装すべきでしょう。
  • あとでコードをリファクタリングするときに、メソッドを汎用化することもできます。
  • すべてを汎用化することはできません。あまりにも汎用化しすぎると生産性が失われてしまうかもしれません。かえってコードが読みにくくなったり、保守性が低下したり、メモリを過大に消費したり、処理時間が増えたりすることがあります。

In this video, we're going to learn how to call methods and use parameters.

We'll cover pointer type parameters later on.

The purpose of generic programming is to optimize development and to facilitate maintenance and portability.

We've already used a simple call to a method (without parameters) in the "On Startup" database method where we called the NAVIGATION method.

In the first programming lessons, we saw how to configure buttons according to the page selected in the navigation form.

Now we're going to optimize this programming so as to keep only one method that is used for all the navigation buttons (All, Query, Order by, and so on).

  • Let's display the Explorer
  • We're going to create a new method
  • A project method that we will name NAVIGATION_FUNCTIONS.

It will receive 2 parameters:

  • The first concerns the action to perform in the form of text (Search, Order by, etc.)
  • The second corresponds to the page where the click occurs (1, 2, 3 and so on)

We call this method as follows:

In a button, we will call NAVIGATION_FUNCTIONS

  • The text corresponding to the action that we want to perform
  • and here, as the 2nd parameter, is the page concerned.

NAVIGATION_FUNCTIONS("All";FORM Get current page)
So we want to use the navigation functions concerning the "All" item and passing the number of the page to process as the 2nd parameter.

There are two ways to type the parameters of the method that is called, in other words, the NAVIGATION_FUNCTIONS method:

  • The 1st parameter will be a text: C_TEXT($1)
  • The 2nd will be a longint: C_LONGINT($2)

You'll notice that to name parameters, we use the syntax $1, $2, ... $n to retrieve each parameter.

To make your method easy to read, we suggest that you create local variables with explicit names where you assign the value of the parameters received. So it is better to write:

 C_TEXT($1;$Action)

since we can declare several variables in a compiler directive of the C_TEXT type for simple variables.
Here is the 2nd variable which will be the page number:

 C_LONGINT($2;$PageNum)

and to retrieve the value of $1  in $Action and the value of $2 in $PageNumber, you can just write:

 $Action:=$1
 $PageNumber:=$2

The rest of the method is an adaptation of what we already described in previous sections; namely, adjusting the action to the request and configuring it according to the page and thus the table chosen:

We will use:

  • a Case of

:($Action="All")

  • and here we will retrieve the code of the "All" button
  • Cut
  • and adapt it in the method.

What about the information we need to replace?

  • The FORM Get current page is no longer needed since it is sent by the calling method. We'll replace it by $PageNumber
  • For the moment, we don't need to change the rest.

When we cover pointers, then we'll replace what we have here with a more generic method.

We can do the same thing with the Query button.

  • Here we drag the code by holding down ALT to copy it
  • Then we'll have an "All" action and a "Query" action
  • We'll copy the code from the "All" button
  • Paste it into the "Query" button
  • Replace the action here
  • Copy the code from the "Query" button
  • And go back to the method here

and, as we did before, replace FORM Get current page by the variable that we received.

We're going to put a break point and check that we go to this method when the form is executed.

If we click on "All", we see the method being executed but what we're specifically interested in is the stacking of the methods:

  • the last one called is at the top
  • the previous one is on the bottom.

If we double-click on this method, the code appears that leads us to this point and we can see that the NAVIGATIONS_FUNCTIONS method was called (with the parameters: "All", FORM Get current page). The method was received. Here it received the parameters: $1 = "All", $2 = the page number--here it is page 1.

Next, $Action is "All" so we're supposed to go to this part
Then $PageNumber is 1, so we will use ALL RECORDS on the Interventions table to determine the number of records concerned.

We will then:

  • Remove the break point
  • Execute the method

When we get to Technician, if we search for those whose name contains "bon", we will find BONAPARTE.
and if we request "All"

We can see that our method works correctly.

 
 

 
プロパティ 

プロダクト: 4D
テーマ: 汎用プログラミング (ポインター不使用)

 
履歴 

 
ARTICLE USAGE

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