全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1533
推到 Plurk!
推到 Facebook!

这种是什么用法?

尚未結案
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-01-10 00:25:15 IP:217.43.xxx.xxx 未訂閱
Type Tfunction=function():string of object; end; 这种是什么用法?
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-01-10 01:22:00 IP:217.43.xxx.xxx 未訂閱
Unit1:    unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Unit2;    type
  TCBnames=(Cb1,Cb2,Cb3);
  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;        procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    //TCBnames=('Combobox1','Combobox2','Combobox3');
    function getFlowUnit(CBname:TCBnames):string;
    //function getFlowUnit:string;
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    { TForm1 }    (*function TForm1.getFlowUnit;
begin      result:=combobox1.text;
end;   *)    procedure TForm1.Button1Click(Sender: TObject);
var
  FU:Tepaswmm;
begin
  FU:=Tepaswmm.create;
  FU.getflowunit:=self.getflowunit(cb3);
  //fu.getflowunit:=ComboBox1.text;
 //FU.getflowunit:=self.getflowunit  ;
  Fu.dosomething;
end;    function TForm1.getFlowUnit(CBname: TCBnames): string;
begin
  case CBname of
    Cb1: result:=ComboBox1.text;
    Cb2: result:=ComboBox2.Text;
    Cb3: result:=ComboBox3.Text;
  end;    end;     end.    Unit2:    unit Unit2;    interface
uses
dialogs;    type
  TgetFlowUnit=function(): string of object;    type
  Tepaswmm=class
  private
  FgetFlowUnit:TgetFlowUnit;
  public
  property  getflowunit:TgetFlowUnit read FgetFlowUnit write FgetFlowUnit;
  procedure Dosomething;
end;
implementation    { Tepaswmm }    procedure Tepaswmm.Dosomething;
begin
  showmessage(FgetFlowUnit);
end;    end.    why cant it be compiled?
the compiler said [Error] Unit1.pas(47): Incompatible types: 'TgetFlowUnit' and 'String'    what i want to do is transfer ComBoBox1.text,ComBoBox2.text,ComBoBox3.text in Unit2's dosomething's procedure.    hope someone can help me !
thanx!
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-01-10 10:34:54 IP:202.39.xxx.xxx 未訂閱
type
  TGetFlowUnit = function(): string of object;
是指宣告了一個型態為 TGetFlowUnit 的 function, 其回傳值型態為 string 而這種 function 須用在 TObject(或後代) 也就是它是一個物件的 method
procedure TForm1.Button1Click(Sender: TObject);
var
  FU: Tepaswmm;
begin
  FU := Tepaswmm.create;
  FU.GetFlowUnit :=  self.GetFlowUnit(cb3);
  FU.GetFlowUnit 其宣告是 property GetFlowUnit: TGetFlowUnit 
  也就是其型態為 TGetFlowUnit
 但是 Self.GetFlowUnit 的回傳值型態為 string 
 兩者型態不同, 所以會有 Incompatible types: 'TgetFlowUnit' and 'String' 的錯誤出現
 
 其實上面 FU.GetFlowUnit :=  動作與如下的動作是類似的
 Form1.Button1Click := Button1Click;
 只是型態不同而己
end;
-- QBQ: 我能做什麼?
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-01-10 17:47:25 IP:213.106.xxx.xxx 未訂閱
What should I do to make these code compile successfully?
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-01-10 18:51:08 IP:202.39.xxx.xxx 未訂閱
試試:
begin
  // ...
  FU.GetFlowUnit := Form1GetFlowUnit;
  ShowMessage(FU.GetFlowUnit(cb3));      // ...
end;
-- QBQ: 我能做什麼?
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-01-10 20:00:08 IP:213.106.xxx.xxx 未訂閱
I HAVE MADE SOME CHANGES: REDECLARE type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; end; but compiler said :[Error] combotest2.pas(28): Not enough actual parameters. showmessage(FgetFlowUnit); //it said not enough actual parameters here; Therefore I change it like: showmessage(FgetFlowUnit(Cb1)); but compiler said: [Error] combotest1.pas(61): Incompatible types: 'TgetFlowUnit' and 'String' FU.getflowunit:=self.getflowunit(cb1); //Incompatible types: 'TgetFlowUnit' and 'String'. The details code are: Unit1: unit combotest1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,combotest2, ComCtrls; type TCBnames=(Cb1,Cb2,Cb3); TForm1 = class(TForm) Button1: TButton; ComboBox1: TComboBox; ComboBox2: TComboBox; ComboBox3: TComboBox; Button2: TButton; Edit1: TEdit; UpDown1: TUpDown; procedure Button1Click(Sender: TObject); private function getFlowUnit(CBname:TCBnames):string; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } function TForm1.getFlowUnit(CBname: TCBnames): string; begin case CBname of Cb1: result:=ComboBox1.text; Cb2: result:=ComboBox2.Text; Cb3: result:=ComboBox3.Text; end; end; procedure TForm1.Button1Click(Sender: TObject); var FU:Tepaswmm; begin FU:=Tepaswmm.create; FU.getflowunit:=self.getflowunit(cb1); Fu.dosomething; end; end. Unit2: unit combotest2; interface uses dialogs; type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; //TgetFlowUnit=function(): string of object; type Tepaswmm=class private FgetFlowUnit{,Fgetcombox2,Fgetcombox3}:TgetFlowUnit; public property getflowunit:TgetFlowUnit read FgetFlowUnit write FgetFlowUnit; procedure Dosomething; end; implementation { Tepaswmm } procedure Tepaswmm.Dosomething; begin showmessage(FgetFlowUnit(Cb1)); end; end. Hope you can slove this problem for me! thanx
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-01-10 20:12:36 IP:213.106.xxx.xxx 未訂閱
I HAVE MADE SOME CHANGES: REDECLARE type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; end; but compiler said :[Error] combotest2.pas(28): Not enough actual parameters. showmessage(FgetFlowUnit); //it said not enough actual parameters here; Therefore I change it like: showmessage(FgetFlowUnit(Cb1)); but compiler said: [Error] combotest1.pas(61): Incompatible types: 'TgetFlowUnit' and 'String' FU.getflowunit:=self.getflowunit(cb1); //Incompatible types: 'TgetFlowUnit' and 'String'. The details code are: Unit1: unit combotest1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,combotest2, ComCtrls; type TCBnames=(Cb1,Cb2,Cb3); TForm1 = class(TForm) Button1: TButton; ComboBox1: TComboBox; ComboBox2: TComboBox; ComboBox3: TComboBox; Button2: TButton; Edit1: TEdit; UpDown1: TUpDown; procedure Button1Click(Sender: TObject); private function getFlowUnit(CBname:TCBnames):string; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } function TForm1.getFlowUnit(CBname: TCBnames): string; begin case CBname of Cb1: result:=ComboBox1.text; Cb2: result:=ComboBox2.Text; Cb3: result:=ComboBox3.Text; end; end; procedure TForm1.Button1Click(Sender: TObject); var FU:Tepaswmm; begin FU:=Tepaswmm.create; FU.getflowunit:=self.getflowunit(cb1); Fu.dosomething; end; end. Unit2: unit combotest2; interface uses dialogs; type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; //TgetFlowUnit=function(): string of object; type Tepaswmm=class private FgetFlowUnit{,Fgetcombox2,Fgetcombox3}:TgetFlowUnit; public property getflowunit:TgetFlowUnit read FgetFlowUnit write FgetFlowUnit; procedure Dosomething; end; implementation { Tepaswmm } procedure Tepaswmm.Dosomething; begin showmessage(FgetFlowUnit(Cb1)); end; end. Hope you can slove this problem for me! thanx
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-01-10 20:13:13 IP:213.106.xxx.xxx 未訂閱
I HAVE MADE SOME CHANGES: REDECLARE type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; end; but compiler said :[Error] combotest2.pas(28): Not enough actual parameters. showmessage(FgetFlowUnit); //it said not enough actual parameters here; Therefore I change it like: showmessage(FgetFlowUnit(Cb1)); but compiler said: [Error] combotest1.pas(61): Incompatible types: 'TgetFlowUnit' and 'String' FU.getflowunit:=self.getflowunit(cb1); //Incompatible types: 'TgetFlowUnit' and 'String'. The details code are: Unit1: unit combotest1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,combotest2, ComCtrls; type TCBnames=(Cb1,Cb2,Cb3); TForm1 = class(TForm) Button1: TButton; ComboBox1: TComboBox; ComboBox2: TComboBox; ComboBox3: TComboBox; Button2: TButton; Edit1: TEdit; UpDown1: TUpDown; procedure Button1Click(Sender: TObject); private function getFlowUnit(CBname:TCBnames):string; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } function TForm1.getFlowUnit(CBname: TCBnames): string; begin case CBname of Cb1: result:=ComboBox1.text; Cb2: result:=ComboBox2.Text; Cb3: result:=ComboBox3.Text; end; end; procedure TForm1.Button1Click(Sender: TObject); var FU:Tepaswmm; begin FU:=Tepaswmm.create; FU.getflowunit:=self.getflowunit(cb1); Fu.dosomething; end; end. Unit2: unit combotest2; interface uses dialogs; type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; //TgetFlowUnit=function(): string of object; type Tepaswmm=class private FgetFlowUnit{,Fgetcombox2,Fgetcombox3}:TgetFlowUnit; public property getflowunit:TgetFlowUnit read FgetFlowUnit write FgetFlowUnit; procedure Dosomething; end; implementation { Tepaswmm } procedure Tepaswmm.Dosomething; begin showmessage(FgetFlowUnit(Cb1)); end; end. Hope you can slove this problem for me! thanx
wuabc
初階會員


發表:6
回覆:60
積分:33
註冊:2002-10-28

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-01-10 23:18:40 IP:203.204.xxx.xxx 未訂閱
引言: I HAVE MADE SOME CHANGES: REDECLARE type TCBnames=(Cb1,Cb2,Cb3); TgetFlowUnit=function(CB:TCBnames): string of object; end; but compiler said :[Error] combotest2.pas(28): Not enough actual parameters. showmessage(FgetFlowUnit); //it said not enough actual parameters here; Right here you can say that you want to call a FUNCTION to get a string result, so, when you call the FUNCTION you need to pass a parameter i.e. CB that you defined above Therefore I change it like: showmessage(FgetFlowUnit(Cb1)); but compiler said: [Error] combotest1.pas(61): Incompatible types: 'TgetFlowUnit' and 'String' FU.getflowunit:=self.getflowunit(cb1); //Incompatible types: 'TgetFlowUnit' and 'String'. And here, you just tell FU that 'self.getflowunit' is you are, that means you just assign a method (a pointer) to FU.getflowunit only, so you don't need any parameter with, like as: FU.getflowunit := self.getflowunit;
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-01-11 00:23:16 IP:213.106.xxx.xxx 未訂閱
I did it like wuabc said: FU.getflowunit:=self.getflowunit; [Error] combotest1.pas(68): Incompatible types: 'combotest2.TCBnames' and 'combotest1.TCBnames' Hope somebody can help me out!
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-01-11 01:03:55 IP:203.204.xxx.xxx 未訂閱
引言: [Error] combotest1.pas(68): Incompatible types: 'combotest2.TCBnames' and 'combotest1.TCBnames'
同樣是TCBnames 的型態,但一個是Combotest1 Unit 中的TCBNames 另一個卻是ComboTest2 Unit 中的TCBnames ,看起來一樣其實對Delphi來說是不一樣的型態 建議你將Combotest1的 Type TCBnames=(Cb1,Cb2,Cb3); 這行定義拿掉即可, 因為Combotest1 有uses 到Combotest2,所以其中的TCBnames會用到同樣是Combotest2裏面定義的TCBnames。 _______________________________________ <>深藍的魚,祝您好運..........連連
系統時間:2024-11-25 2:44:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!