Tuesday, August 25, 2020

Understanding Delphi Class (and Record) Helpers

Understanding Delphi Class (and Record) Helpers A component of the Delphi language included a few years prior (route back in Delphi 2005) called Class Helpers is intended to let you add new usefulness to a current class (or a record) by acquainting new techniques with the class (record). Beneath youll see some more thoughts for class partners realize when to and when not to utilize class aides. Class Helper For... In straightforward words, a class aide is a develop that broadens a class by presenting new strategies in the aide class. A class assistant permits you to broaden existing class without really changing it or acquiring from it. To expand the VCLs TStrings class you would pronounce and execute a class partner like the accompanying: type TStringsHelper class aide for TStrings open work Contains(const aString : string) : boolean; end; The above class, called TStringsHelper is a class aide for the TStrings type. Note that TStrings is characterized in the Classes.pas, a unit that is as a matter of course accessible in the utilizations provision for any Delphi structures unit, for instance. The capacity were adding to the TStrings type utilizing our class partner is Contains. The execution could resemble: work TStringsHelper.Contains(const aString: string): boolean; start result : - 1 IndexOf(aString); end; Im certain youve utilized the above commonly in your code - to check if some TStrings relative, similar to TStringList, makes them string an incentive in its Items assortment. Note that, for instance, the Items property of a TComboBox or a TListBox is of the TStrings type. Having the TStringsHelper actualized, and a rundown box on a structure (named ListBox1), you would now be able to check if some string is a piece of the rundown box Items property by utilizing: on the off chance that ListBox1.Items.Contains(some string) at that point ... Class Helpers Go and NoGo The usage of class partners has some positive and a few (you may consider) negative effects on your coding. By and large you ought to abstain from expanding your own classes - as though you have to add some new usefulness to your own custom classes - include the new stuff in the class usage legitimately - not utilizing a class aide. Class aides are along these lines progressively intended to broaden a class when you can't (or don't have to) depend on ordinary class legacy and interface executions. A class assistant can't pronounce example information, as new private fields (or properties that would peruse/compose such fields). Including new class fields is permitted. A class aide can include new strategies (work, method). Before Delphi XE3 you could just broaden classes and records - complex sorts. From Delphi XE 3 discharge you can likewise expand straightforward sorts like number or string or TDateTime, and have build like: var s : string; start s : Delphi XE3 partners; s : s.UpperCase.Reverse; end; Sick expound on Delphi XE 3 basic sort partner sooner rather than later. Wheres MY Class Helper One restriction to utilizing class partners that may assist you with messing yourself up is the way that you can characterize and connect different assistants with a solitary sort. In any case, just zero or one partner applies in a particular area in source code. The assistant characterized in the closest extension will apply. Class or record aide degree is resolved in the typical Delphi design (for instance, option to left in the units utilizes provision). This means you may characterize two TStringsHelper class partners in two distinct units however just one will apply when really utilized! On the off chance that a class assistant isn't characterized in the unit where you utilize its presented techniques - which by and large will be in this way, you don't have the foggiest idea what class partner usage you would really be utilizing. Two class aides for TStrings, named distinctively or living in various units may have diverse usage for the Contains technique in the above model. Use Or Not? Truly, yet know about the conceivable reactions. Heres another convenient expansion to the previously mentioned TStringsHelper class aide TStringsHelper class aide for TStrings private work GetTheObject(const aString: string): TObject; system SetTheObject(const aString: string; const Value: TObject); open property ObjectFor[const aString : string]: TObject read GetTheObject compose SetTheObject; end; ... work TStringsHelper.GetTheObject(const aString: string): TObject; var idx : whole number; start result : nil; idx : IndexOf(aString); in the event that idx - 1, at that point result : Objects[idx]; end; strategy TStringsHelper.SetTheObject(const aString: string; const Value: TObject); var idx : whole number; start idx : IndexOf(aString); on the off chance that idx - 1, at that point Objects[idx] : Value; end; On the off chance that youve been adding items to a string show, you can think about when to utilize the above convenient assistant property.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.