views:

434

answers:

3

Is there any way with the current delphi to implement.

a) String (as a class) with operator overloads (ie. +, =)

b) Class Helper's so one could add custom string methods

I gather the string is a native type so class helpers will not work without setting up a class etc.

+2  A: 

Isn't a better solution to write your custom functions / procedures?

For example

Function StrToBase64(AString): string;
Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
Fuction ExtractWord(aString): string;
Function GetStrColumn(aString: string; aCol: integer): string;

And if you want to group these functions / procedures which reside in the same unit in functional categories you can use records for this:

TStringConversions = record
  class Function StrToBase64(AString): string;
  class Procedure StrToGridLayout(AString: string; AGrid: TDBGrid);
end;

TStringParsing = record
  class Fuction ExtractWord(aString): string;
  class Function GetStrColumn(aString: string; aCol: integer): string;
end;

And then, you can call them in code in a much cleaner way:

myFirstWord := TStringParsing.ExtractWord('Delphi is a very good tool');

HTH

+3  A: 

Yes, string is a native type with some special compiler magic added.

I don't know what operator overloading you would want. + and = already work as concatenation and equality operators.

However I've thought about doing something similar myself. It might work with a record type with implicit convertors and overloaded add and equals operators (in Win32 Delphi only records can have operator overloading. This is available in D2006 (?2005) only.)

I suspect there may be some performance hit as well.

The syntax would be something like the following:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

   TString = record
    private
      Value : string;
    public
      class operator Add(a, b: TString): TString;
      class operator Implicit(a: Integer): TString;
      class operator Implicit(const s: string): TString;
      class operator Implicit(ts: TString): String;
      function IndexOf(const SubStr : string) : Integer;
    end;


var
  Form1: TForm1;

implementation

class operator TString.Add(a, b : TString) : TString;
begin
  Result.Value := a.Value + b.Value;
end;

class operator TString.Implicit(a: Integer): TString;
begin
  Result.Value := IntToStr(a);
end;

class operator TString.Implicit(ts: TString): String;
begin
  Result := ts.Value;
end;

function TString.IndexOf(const SubStr : string) : Integer;
begin
  Result := Pos(SubStr, Value);
end;

class operator TString.Implicit(const s: string): TString;
begin
  Result.Value := s;
end;


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  ts : TString;
begin
  ts := '1234';
  ShowMessage(ts);
  ShowMessage(IntToStr(Ts.IndexOf('2')));
end;

end.

Apparently you can have "record helpers" as well, but I've never tried it myself.

Gerry
You can create record helpers too.
Mason Wheeler
ou learn something new everyday :)
Gerry
+1  A: 

You can use operator overloading in Delphi (since Delphi 2006) only on records not on classes, and not on built-in native types like strings.

The reason is that Delphi does not have garbage collection, so operator overloading is limited to value types (types that are not living on the heap).

You can download the replay of my session "Nullable Types with Records, Methods and Operator Overloading" at the CodeRage III Replay download page. Just search for the session name.

There is also a page with the download for the session samples and slides.

It contains quite a few examples that get you going, including a description of some issues in the Delphi 2006 compiler that have been solved in Delphi 2007 and up.

See also this question: http://stackoverflow.com/questions/329359/can-i-overload-operators-for-my-own-classes-in-delphi

Jeroen Pluimers