我在delphi中有SelectTable:integer是private
还有函数SetSelect和GetSelect是public
请问该怎么做啊?
另外,下面是我写的SetSelect和GetSelect程序,请大家看看对不对啊,谢谢。
我觉得有问题,但不知道问题出在哪里。
Procedure SetSelect()
Begin
SelectTable:=1;
End;Function GetSelect():Integer;
Begin
Result:=SelectTable;
End;

解决方案 »

  1.   

    Function Form1.GetSelect():Integer;
      

  2.   

    TMyClass = class
    private
      FSelectTable: Integer;
      function GetSelect: Integer;
      procedure SetSelect(Value: Integer);
    public  // 建议用 published
      property SelectTable: Integer read GetSelect write SetSelect;
    end;procedure TMyClass.SetSelect(Value: Integer)
    begin
      FSelectTable := Value;
    end;function TMyClass.GetSelect:Integer;
    begin
      Result := FSelectTable;
    end;
      

  3.   

    其实,楼主只要写:
    property SelectTable: Integer read GetSelect write SetSelect;
    然后按 Ctrl + Shift + C,SetSelect、GetSelect 函数的声明、定义就自动给出了:
    procedure TMyClass.SetSelect(Value: Integer)
    beginend;function TMyClass.GetSelect:Integer;
    beginend;
    方便吧?btw, 楼主是从 Basic 转过来的?
      

  4.   

    看雨飞:你好,我按照你的方法修改了程序,还是通不过;
    报错:expected ‘:’ but ‘=’ found
    我把程序罗列如下,你看看是不是程序修改的位置不对啊?
    unit Employee_Find;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DBCtrls, StdCtrls, ExtCtrls, Grids, DBGrids, Mask;type
        TF_Employee_find = class(TForm)    
    TMyclass = class
    ……  private
             FSelectTable:Integer;
      public
         property SelectTable: Integer read GetSelect write SetSelect;
         function GetSelect: Integer;
         procedure SetSelect(Value: Integer);
    end;var
      F_Employee_find: TF_Employee_find;
      
    implementationuses U_DataModuleMain,employee;{$R *.dfm}procedure TMyClass.SetSelect(Value:Integer);
    begin
      FSelectTable:=Value;
    end;function TMyClass.GetSelect:integer;
    begin
        Result:=FSelectTable;
    end;
      

  5.   

    你不会是把 TMyclass = class ... 写到了 TF_Employee_find 的定义之内吧?
      

  6.   

    看雨飞:你好,刚才的确把 TMyclass = class ... 写到了 TF_Employee_find 的定义之内,
    谢谢你的提醒。我把程序修改如下:
    type
        TMyclass = class
      private
         FSelectTable:Integer;
      public
         property SelectTable: Integer read GetSelect write SetSelect;   //第51行
         function GetSelect: Integer;
         procedure SetSelect(Value: Integer);
    end;
    但是为何property SelectTable: Integer read GetSelect write SetSelect;此句通不过。
    报错信息:[Error] Employee_Find.pas(51): Field or method identifier expected
    我也不知道这句是什么意思,能简单的解释一下吗?
    我看过help了,相关property的内容很多,看得云里雾里的。
    谢谢。
      

  7.   

    type
    TMyclass = class
    private
    FSelectTable:Integer;
    protected
    function GetSelect: Integer;
    procedure SetSelect(Value: Integer);
    published
    property SelectTable: Integer read GetSelect write SetSelect; //第51行
    end;
    implementation
    function TMyclass.GetSelect: Integer;
    begin
    result:=FSelectTable;
    end;
    procedure TMyclass.SetSelect(Value: Integer);
    begin
    FSelectTable:=value;
    end;
      

  8.   

    getit911:你好,按你这么一改,编译通过了,不过还是有两个问题。1、在这个pas中有两个类TF_Employee_find = class(TForm)和TMyclass = class
       如何在procedure TF_Employee_find.btn_cancelClick(Sender: TObject);中
       使用TMyClass.SetSelect(2)?2、若把GetSelect和SetSelect定义成Protected,那岂不是别的pas文件就无法调用它们两个了?
       我需要别的pas也可以调用它们,程序应如何改动?请指教,谢谢。
      

  9.   

    1. 为什么要使用 TMyClass.SetSelect(2)? SetSelect 又不是静态方法!
       既然是用了 property 机制,就没必要调 SetXXX 了,直接写:
    var 
      MyObj: TMyClass;
    begin
      MyObj := TMyClass.Create;
      MyObj.SelectTable := 1234;  // <<<<<
      MyObj.Free;
    end;   2. 虽然GetSelect和SetSelect是Protected,但是 SelectTable 确是 published 的,可以在别的单元里读写。