Delphi – generic type name

I have this code:

TServer = class
private
fOnMsgFromServer: TProc< String, String, String>;
public
procedure RegisterOnMsgFromServer(aCallBack: TProc);
procedure Execute;
end;

procedure TServer.RegisterOnMsgFromServer(aCallBack: TProc);
begin
fOnMsgFromServer := aCallBack;
end;

procedure TServer. Execute;
begin
fOnMsgFromServer('userName','password','message');
end;

The problem lies in the program execution when I want to put the parameters in fOnMsgFromServer. “Helper” shows me (Arg1: string; Arg2: string; Arg3: string), I just don’t know which parameter is which.

Is there a solution to name this argument?

If you use generic TProc, these generic names cannot be avoided. . Its declaration is as follows:

type
TProc = reference to procedure (Arg1: T1; Arg2: T2; Arg3 : T3);

As you can see, this is the origin of the names. If you use this type, you will encounter these names.

Instead, you should declare a custom reference Process types instead of using generic types. This not only allows you to impart meaningful names to the argument, but also allows you to repeat yourself over and over again.

type
TMsgFromServerProc = reference to procedure(UserName, Password, Msg: string);

TServer = class
private
fOnMsgFromServer: TMsgFromServerProc;
public
procedure RegisterOnMsgFromServer(aCallBack: TMsgFromServerProc);
end;

I have this code:

< pre>TServer = class
private
fOnMsgFromServer: TProc;
public
procedure RegisterOnMsgFromServer(aCallBack: TProc);< br /> procedure Execute;
end;

procedure TServer.RegisterOnMsgFromServer(aCallBack: TProc);
begin
fOnMsgFromServer := aCallBack;
end;

procedure TServer.Execute;
begin
fOnMsgFromServer(‘userName ‘,’password’,’message’);
end;

The problem is that the program is executed when I want to put the parameters in fOnMsgFromServer. “helper” shows me (Arg1: string; Arg2: string; Arg3: string), I just don’t know which parameter is which.

Is there a solution to name this argument?

If you use generic TProc, these generic names and types cannot be avoided. Its declaration is as follows:

type
TProc = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3);

As you can see, this is the origin of the names. If you use this type, you will encounter these names.

Instead, you should declare a custom reference procedure type instead of using a generic type. This is not only It allows you to impart meaningful names to the argument, and it allows you to repeat yourself constantly.

type
TMsgFromServerProc = reference to procedure(UserName, Password, Msg: string);

TServer = class
private
fOnMsgFromServer: TMsgFromServerProc;
public
procedure RegisterOnMsgFromServer(aCallBack: TMsgFromServerProc);
end;

Leave a Comment

Your email address will not be published.