-1

I came from Delphi 2007, have been using this for 7 years and started working with RAD Studio Berlin in my new work place. The projects I am working on use a lot of TDictionary, Generics collections, asynchronous...etc. For examaple, there is a class:

TMyBaseClass = class abstract(TObject)
private
...

end;

TNewClassGeneric<T: TMyBaseClass> = class(TMyAbstractSuperClass)
....

So what does this TNewClassGeneric mean?

I am still struggling about these new things to me. Any pointer I can read more about those generic, dictionary and asynchronous program code samples? Thanks

Ricky Nelson
  • 197
  • 1
  • 1
  • 7

1 Answers1

0

Look at the class below below (It should compile). Notice that it just uses a genric T and doesn't specify a type in it's property definition for MyValue. This allows it to be used as either an integer or string in the two procedures, UseGenreicInt & UseGnericString, the demonstrate what it can do. Thats the power of generics. You can define a class to act on data without knowing in advance what type of data - int, string etc - you will be dealing with.

unit Sample.Generic;

interface

uses
  System.SysUtils, System.Variants, System.Classes, System.Generics.Collections, VCL.Dialogs;

type
  TSampleGeneric<T> = class
  protected
    FValue: T;
  public
    constructor Create(AValue: T);
    property MyValue: T read FValue write FValue;
  end;

procedure UseGenricString;
procedure UseGenricInt;

implementation

constructor TSampleGeneric<T>.Create(AValue: T);
begin
  FValue := AValue;
end;

procedure UseGenricInt;
var
  LSample: TSampleGeneric<Integer>;
begin
  LSample := TSampleGeneric<Integer>.Create(100);
  try
    ShowMessage(IntToStr(LSample.MyValue));
  finally
    LSample.Free;
  end;
end;

procedure UseGenricString;
var
  LSample: TSampleGeneric<String>;
begin
  LSample := TSampleGeneric<String>.Create('ATestString');
  try
    ShowMessage(LSample.MyValue);
  finally
    LSample.Free;
  end;
end;

end.
Tav
  • 336
  • 1
  • 6