hi,

I've made a little example that works fine, but i don't know if it is what 
you were expecting.
The problem is :
if you replace all my "class function" with simple functions, it still 
works...
so my example do not show the using of generic reference of class you were 
attending ;-)


unit Unit1;

interface

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

type

  enumFormatter = (
    ftArrangementNumber,
    ftCustomerNumber
  );

  TForm1 = class(TForm)
    btnFormat: TButton;
    rgFormatType: TRadioGroup;
    procedure btnFormatClick(Sender: TObject);
  private
    { Déclarations privées }
    procedure Test(AFormatType : enumFormatter);
  public
    { Déclarations publiques }
  end;


  TFormatter = class
  public
    class function FormatString(aString: String): string; virtual;
  end;

  TFormatterClass = class of TFormatter;

  TCustomerNumberFormatter = class (TFormatter)
  public
    class function FormatString(aString: String): string; override;
  end;

  TArrangementNumberFormatter = class (TFormatter)
  public
    class function FormatString(aString: String): string; override;
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}


class function TFormatter.FormatString(aString: String): string;
begin
end;

class function TCustomerNumberFormatter.FormatString(aString: String): 
string;
begin
  Result := 'CUST : ' + aString + '*';
end;

class function TArrangementNumberFormatter.FormatString(aString: String): 
string;
begin
  Result := 'ARR : ' + aString + '*';
end;

procedure TForm1.Test(AFormatType : enumFormatter);
var
  myClass : TFormatterClass;
  ftClass : TFormatter;
begin
  myClass := nil;

  case AFormatType of
    ftArrangementNumber : myClass := TArrangementNumberFormatter;
    ftCustomerNumber : myClass := TCustomerNumberFormatter;
  end;
  ftClass := myClass.Create;

  ShowMessage(ftClass.FormatString('Text to format'));
end;

procedure TForm1.btnFormatClick(Sender: TObject);
begin
  Test(enumFormatter(rgFormatType.ItemIndex));
end;

end.


Cédric


----- Original Message ----- 
From: "Jesper Stenlund" <jes...@handelsbanken.se>
To: <delphi@elists.org>
Sent: Tuesday, May 12, 2009 1:20 PM
Subject: Class method


>I have a bunch of classes that formats a string in different ways.
> Today I have to create an instance of a specific "formattingobject" and
> then run the formattingmethod.
> Instead of that behaviour I now want make the formattingmethod a class
> method.
>
> But that causes me some other problems.
> Here's a small example:
>
> TCustomerNumberFormatter = class (TObject)
> private
>  class function FormatString(aString: String): string;
> end;
>
> TArrangementNumberFormatter = class (TObject)
> private
>  class function FormatString(aString: String): string;
> end;
>
>
> Then I want to do something like this but I don't know exactly how to
> write my code.
>
> case FormatType of
>  ftArrangementNumber: myClass := TArrangementNumberFormatter;
>  ftCustomerNumber: myClass := TCustomerNumberFormatter;
> end;
>
> TEdit1.Caption := myClass.FormatString(myObject.StringToFormat);
>
>
> I know that last row isn't correct, but how can I do this?
>
> I hope you understand what I'm looking for.
> I want to use the classname that I get from the case-sentence and then run
> the class function on that particular class.
> Is it possible to do it like this?
>
>
> //Jesper
> _______________________________________________
> Delphi mailing list -> Delphi@elists.org
> http://lists.elists.org/cgi-bin/mailman/listinfo/delphi 

_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to