Cos'è l'opzione Index nelle classi derivate TRemotable?
Domanda
Quando la procedura guidata dell'importatore WSDL genera le interfacce, tutte le proprietà hanno l'opzione Index, ma leggendo il codice e l'unità InvokeRegistry, non riesco a trovare quello per cui, qualcuno sa se è davvero necessario?
In questo modo
Login = class(TRemotable)
private
[...]
published
property User: string Index (IS_OPTN) read GetUser write SetUser stored User_Specified;
[...]
end;
Chiedo perché voglio cambiare questa unità, aggiungendo alcune interfacce a queste classi, per l'integrazione con il framework MVP.
Soluzione 2
Ho trovato una spiegazione più dettagliata per questa domanda, Quando si utilizzano gli indici, diverse proprietà possono condividere gli stessi metodi di accesso.
Un buon esempio, dalla guida di Delphi 2009:
type
TRectangle = class
private
FCoordinates: array[0..3] of Longint;
function GetCoordinate(Index: Integer): Longint;
procedure SetCoordinate(Index: Integer; Value: Longint);
public
property Left: Longint index 0 read GetCoordinate write SetCoordinate;
property Top: Longint index 1 read GetCoordinate write SetCoordinate;
property Right: Longint index 2 read GetCoordinate write SetCoordinate;
property Bottom: Longint index 3 read GetCoordinate write SetCoordinate;
property Coordinates[Index: Integer]: Longint read GetCoordinate write SetCoordinate;
...
end;
Nota, tutte le proprietà condividono l'accesso allo stesso metodo.
Altri suggerimenti
IS_OPTN viene passato a GetUser e SetUser tramite il parametro 'Index' quando si accede alla proprietà dell'utente.
I getter / setter probabilmente assomigliano a questo:
function GetUser(Index:Integer):String;
procedure SetUser(Index:Integer;const value:string);
Quindi, pensala come questa:
MyString := MyLogin.user;
// is translated to:
MyString := getUser(IS_OPTN);
e
MyLogin.user := 'me';
// is translated to:
SetUser(IS_OPTN,'me');
Sì, è necessario. Con queste informazioni, ad esempio IS_OPTN, la classe da TRemotable sa che quando la proprietà è Opzionale per costruire XML, quindi se è facoltativo, il nodo viene aggiunto solo se il valore è memorizzato. Nel tuo caso:
property User: string Index (IS_OPTN) read GetUser write SetUser stored User_Specified
L'elemento Utente verrà aggiunto su XML se Specifica_utente vero. User_Specified diventa automaticamente vero quando si imposta il valore su User, perché lo ha fatto il setter SetUser.
Quindi, quando il componente SOAP por esempio, costruirà l'XML, l'elemento verrà aggiunto solo se è memorizzato perché è opzionale (IS_OPTN).