代码之家  ›  专栏  ›  技术社区  ›  Cesar Romero

TRemotable派生类中的Index选项是什么?

  •  3
  • Cesar Romero  · 技术社区  · 17 年前

    当WSDL导入向导生成接口时,所有属性都有Index选项,但在读取代码和InvokeRegistry单元时,我找不到它的用途,有人知道它是否真的有必要吗?

    这样地

      Login = class(TRemotable)
      private
        [...] 
      published
        property User: string Index (IS_OPTN) read GetUser write SetUser stored User_Specified;
        [...]
      end;
    

    我问这个问题是因为我想改变这个单元,向这个类添加一些接口,以便与MVP框架集成。

    3 回复  |  直到 17 年前
        1
  •  2
  •   Wouter van Nifterick Andrey    17 年前

    当您访问用户属性时,IS_OPTN通过“Index”参数传递给GetUser和SetUser。

    getter/setter可能看起来像这样:

    function GetUser(Index:Integer):String;
    procedure SetUser(Index:Integer;const value:string);
    

    所以,可以这样想:

    MyString := MyLogin.user;
    // is translated to:
    MyString := getUser(IS_OPTN);
    

    MyLogin.user := 'me'; 
    // is translated to:
    SetUser(IS_OPTN,'me');
    
        2
  •  2
  •   Cesar Romero    16 年前

    我找到了这个问题的更详细的解释, 使用索引时,多个属性可以共享相同的访问方法。

    一个很好的例子,来自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;
    

    请注意,所有属性共享相同的方法访问权限。

        3
  •  0
  •   Roberto Novakosky    9 年前

    是的,这是必要的。有了这些信息,例如IS_OPTN,TRemotable中的类知道当属性是构建XML的可选属性时,如果是可选的,则只有在存储了值时才会添加节点。关于您的案例:

    property User: string Index (IS_OPTN) read GetUser write SetUser stored User_Specified
    

    元素 用户 如果满足以下条件,将添加到XML上 用户_指定 确实如此。这个 用户_指定 当您将值设置为User时,它会自动变为true,因为设置器SetUser会这样做。

    因此,当组件SOAP-por示例构建XML时,只有当元素被存储时才会添加,因为它是可选的(is_OPTN)。

    推荐文章