官术网_书友最值得收藏!

  • Expert Delphi
  • Pawe? G?owacki
  • 388字
  • 2021-07-02 20:44:23

Helpers

Manipulating types in code can be even more readable when we define type helpers. Helpers extend a type vertically. You can define them on simple types, enumerations, records, or even on dynamic arrays. Delphi already comes with helpers defined for many built-in types, so they are easier to use in code. One good example is the very common task of converting an integer to a string, so it could be displayed as shown in the following code:

var s: string; i: integer; 
begin 
  i := 10; 
  s := IntToStr(i); // "old" style 
  s := i.ToString;  // more readable 
  s := 10.ToString; // that would work too 

Just right-click on the ToString method in this code and jump to the declaration. In the System.SysUtils unit, you will see helpers defined for many built-in types providing some additional useful methods, such as the minimum and maximum values for given built-in numerical types.

It is easy to define your own helper classes. Until Delphi 10, using helpers it was possible to access some private functionality of types that come out of the box with Delphi libraries, but since then this hack no longer works.

We already have the TSchoolGrade custom enumerated type. It would be useful to provide methods to convert a school grade value to a string or to a number. With helpers, it can be done in a really elegant way, as shown in the following code:

type 
  TSchoolGrade = (sgVeryGood, sgGood, sgSufficient, sgInsufficient); 
  TSchoolGrades = set of TSchoolGrade; 
 
const 
  QUALIFYING_GRADES: TSchoolGrades = [sgVeryGood, sgGood, sgSufficient]; 
 
type 
  TSchoolGradeHelper = record helper for TSchoolGrade 
  public 
    function ToString: string; 
    function ToInteger: integer; 
    function IsQualifying: boolean; 
  end; 
 
{ TSchoolGradeHelper } 
 
function TSchoolGradeHelper.IsQualifying: boolean; 
begin 
  Result := self in QUALIFYING_GRADES; 
end; 
 
function TSchoolGradeHelper.ToInteger: integer; 
begin 
  case self of 
    sgVeryGood: Result := 5; 
    sgGood: Result := 4; 
    sgSufficient: Result := 3; 
    sgInsufficient: Result := 2; 
  end; 
end; 
 
function TSchoolGradeHelper.ToString: string; 
begin 
  case self of 
    sgVeryGood: Result := 'Very Good'; 
    sgGood: Result := 'Good'; 
    sgSufficient: Result := 'Sufficient'; 
    sgInsufficient: Result := 'Insufficient'; 
  end; 
end; 

We use the self pseudo-variable in the implementation of a helper method to refer to the current value of the type we operate on.

Also, the function to check whether a given grade qualifies has been refactored as a helper method. It is more readable and improves the code structure.

主站蜘蛛池模板: 大宁县| 抚宁县| 来凤县| 曲沃县| 黑水县| 三台县| 政和县| 方正县| 屯留县| 兴仁县| 铁力市| 墨脱县| 长丰县| 肇东市| 凭祥市| 宝清县| 南郑县| 舞钢市| 临城县| 孟津县| 嵩明县| 砀山县| 梓潼县| 秦安县| 崇礼县| 余姚市| 郴州市| 清水河县| 京山县| 大理市| 高邑县| 平遥县| 建德市| 安图县| 法库县| 闵行区| 大庆市| 昔阳县| 金塔县| 泌阳县| 桐乡市|