線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:896
推到 Plurk!
推到 Facebook!

一個不太明白的變數次序的問題

尚未結案
stacker_liew
中階會員


發表:59
回覆:168
積分:65
註冊:2004-05-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2019-01-11 10:26:02 IP:115.134.xxx.xxx 未訂閱
在一個Unit裡有個property Operation: Byte read GetOperation write SetOperation;,底下有個procedure Do_Operation(Sender: TObject),內容如下:
procedure Do_Operation(Sender: TObject);
begin
if Operation = 0 then
begin
...
end
else
begin
...
Something(Operation);
end;
end;

可是奇怪的問題是,在別處呼叫此procedure時,例必先呼叫Do_Operation(Sender),然後再設定Operation的值,請問這種次序對嗎?
例子:
Do_Operation(Sender);
Operation := 1;

我想問的是,那Do_Operation(Sender)被呼叫時,它的Operation的內容是什麼?
這是我在一個計算機的原始碼看到的。
編輯記錄
stacker_liew 重新編輯於 2019-01-11 10:26:44, 註解 無‧
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2019-01-14 13:42:11 IP:59.120.xxx.xxx 未訂閱
沒看到全部的前後關係, 不好說, 但我自己的看法是

Operation 是一個 property, 為 Byte形態, 存取會被 GetOperation, SetPoeration 事件所觸發, 與 Do_Operation 是沒有關係的, 也就是 Do_Operation就是一個procedure , 當設定 Operation := 1 會觸發 Write SetOperation, 至於它與 Do_Operation 有何觸發關連, 看不太出來!

以上純就提供的內容判斷, 對錯與否僅代表本看法!
===================引 用 stacker_liew 文 章===================
在一個Unit裡有個property Operation: Byte read GetOperation write SetOperation;,底下有個procedure Do_Operation(Sender: TObject),內容如下:
procedure Do_Operation(Sender: TObject);
begin
if Operation = 0 then
begin
...
end
else
begin
...
Something(Operation);
end;
end;

可是奇怪的問題是,在別處呼叫此procedure時,例必先呼叫Do_Operation(Sender),然後再設定Operation的值,請問這種次序對嗎?
例子:
Do_Operation(Sender);
Operation := 1;

我想問的是,那Do_Operation(Sender)被呼叫時,它的Operation的內容是什麼?
這是我在一個計算機的原始碼看到的。
stacker_liew
中階會員


發表:59
回覆:168
積分:65
註冊:2004-05-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2019-01-14 18:21:53 IP:115.134.xxx.xxx 未訂閱
那個GetOperation和SetOperation跟我們一般設定最普通的property方法沒有什麼特別,所以應該問題不是在這裡。我只是不明白的是為什麼不是在呼叫Do_Operation(Sender)前先設定Operation,而是在Do_Operation(Sender)後再設定Operation這樣有意義嗎?
stacker_liew
中階會員


發表:59
回覆:168
積分:65
註冊:2004-05-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2019-01-14 18:25:02 IP:115.134.xxx.xxx 未訂閱
unit CalculatorFrm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, math, StdCtrls, ExtCtrls, jpeg;

type
TCalculatorForm = class(TForm)
LabelDisplay: TLabel;
LabelInverse: TLabel;
BgImg: TImage;
Img0: TImage;
Img1: TImage;
img4: TImage;
img5: TImage;
Img2: TImage;
img3: TImage;
img6: TImage;
img7: TImage;
img8: TImage;
img9: TImage;
ImgPlus: TImage;
ImgMin: TImage;
ImgMult: TImage;
ImgDiv: TImage;
ImgEqual: TImage;
ImgClose: TImage;
ImgMinimize: TImage;
PointImg: TImage;
PiImg: TImage;
InvImg: TImage;
XyImg: TImage;
ExpImg: TImage;
SinImg: TImage;
CosImg: TImage;
TanImg: TImage;
XinvImg: TImage;
RootImg: TImage;
X2Img: TImage;
LogImg: TImage;
LnImg: TImage;
PmImg: TImage;
McImg: TImage;
MrImg: TImage;
MpImg: TImage;
CImg: TImage;
AcImg: TImage;
ImgAbout: TImage;
RanImg: TImage;
LabelMemory: TLabel;
RoundImg: TImage;
IntImg: TImage;
FactImg: TImage;
LabelDecimal: TLabel;
procedure FormCreate(Sender: TObject);
procedure BtnDivClick(Sender: TObject);
Procedure Do_Operation(Sender: TObject);
Procedure FindResult(Op: Byte);
procedure Img0MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Img0MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure Img0MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure ImgMinimizeClick(Sender: TObject);
procedure ImgCloseClick(Sender: TObject);
procedure BgImgMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ImgAboutClick(Sender: TObject);
private
{ Private declarations }
FNumb1: Extended;
FNumb2: Extended;
FStoreResult: Extended;
FMem1: Extended;
FOperation: Byte; // 1 = addition, 2 = subtr., 3 = Multip., 4 = div.
FPoint: Boolean;
FOk: Boolean;
FXy: Boolean;
FPressed: Boolean;
function GetNumb1: Extended;
procedure SetNumb1(const Value: Extended);
function GetNumb2: Extended;
procedure SetNumb2(const Value: Extended);
function GetOperation: Byte;
procedure SetOperation(const Value: Byte);
function GetStoreResult: Extended;
procedure SetStoreResult(const Value: Extended);
function GetMem1: Extended;
procedure SetMem1(const Value: Extended);
function GetPoint: Boolean;
procedure SetPoint(const Value: Boolean);
function GetOk: Boolean;
procedure SetOk(const Value: Boolean);
function GetXy: Boolean;
procedure SetXy(const Value: Boolean);
function GetPressed: Boolean;
procedure SetPressed(const Value: Boolean);
public
{ Public declarations }
property Numb1: Extended read GetNumb1 write SetNumb1;
property Numb2: Extended read GetNumb2 write SetNumb2;
property StoreResult: Extended read GetStoreResult write SetStoreResult;
property Mem1: Extended read GetMem1 write SetMem1;
property Operation: Byte read GetOperation write SetOperation;
property Point: Boolean read GetPoint write SetPoint;
property Ok: Boolean read GetOk write SetOk;
property Xy: Boolean read GetXy write SetXy;
property Pressed: Boolean read GetPressed write SetPressed;
end;

var
CalculatorForm: TCalculatorForm;

implementation

uses AboutFrm;

{$R *.dfm}

procedure TCalculatorForm.Do_Operation(Sender: TObject);
begin
if Operation = 0 then
begin
Numb1 := StrToFloat(LabelDisplay.Caption);
end
else
begin
Numb2 := StrToFloat(LabelDisplay.Caption);
FindResult(Operation);
end;
Ok := True;
Point := False;
end;

procedure TCalculatorForm.FindResult(Op: Byte);
begin
case Op of
1:
StoreResult := (Numb1 Numb2);
2:
StoreResult := (Numb1 - Numb2);
3:
StoreResult := (Numb1 * Numb2);
4:
StoreResult := (Numb1 / Numb2);
else
Exit;
end;

Numb1 := StoreResult;

with LabelDisplay do
Caption := FloatToStr(StoreResult);
Operation := 0;
end;

procedure TCalculatorForm.FormCreate(Sender: TObject);
var
I: Byte;
Rgn: THandle;
begin
Rgn := CreateRoundRectRgn(0, 0, BgImg.Width, BgImg.Height, 20, 20);
SetWindowRgn(Handle, Rgn, True);
DeleteObject(Rgn);
with LabelDisplay do
Caption := '0';
Numb1 := 0;
Numb2 := 0;
Mem1 := 0;
Point := False;
Xy := False;
Operation := 0;
StoreResult := 0;
Pressed := False;

for I := 0 to ComponentCount - 1 do
if (Components[I] is TImage) then
if (TImage(Components[I]).Tag < 1000) then
TImage(Components[I]).Picture.Bitmap.LoadFromResourceID(HInstance, (TImage(Components[I]).Tag 1) * 10);
end;

procedure TCalculatorForm.BtnDivClick(Sender: TObject);
begin
if Ok then
Exit;
Do_Operation(Sender);
Operation := 4;
end;

procedure TCalculatorForm.Img0MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button <> MBLeft then
Exit;
TImage(Sender).Picture.Bitmap.LoadFromResourceID(HInstance, (TImage(Sender).Tag 1) * 10 1);
Pressed := True;
end;

procedure TCalculatorForm.Img0MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
I, M: Extended;
begin
if (Button <> MBLeft) or not Pressed then
Exit;

TImage(Sender).Picture.Bitmap.LoadFromResourceID(HInstance, (TImage(Sender).Tag 1) * 10);

case TImage(Sender).Tag of
0 .. 9:
begin // numbers 0..9
with LabelDisplay do
begin
if (Caption = '0') then
Caption := '';
if (Ok and not Point) then
begin
Caption := '';
Ok := False;
end;
if Length(Caption) >= 15 then
Exit;
Caption := Caption IntToStr(TImage(Sender).Tag);
end;
end;
10 .. 13:
begin // opeartions -*/
if Ok then
Exit;
Do_Operation(Sender);
Operation := TImage(Sender).Tag - 9;
end;
14:
begin // equal =
with LabelDisplay do
begin
if (Numb1 = 0) or (Operation = 0) and not Xy then
Exit;
Numb2 := StrToFloat(Caption);
if Operation <> 0 then
FindResult(Operation)
else if Xy then
begin
Numb1 := Power(Numb1, Numb2);
Caption := FloatToStr(Numb1);
with LabelInverse do
Visible := False;
Xy := False;
end;
Point := False;
Numb1 := StrToFloat(Caption);
Numb2 := 0;
StoreResult := 0;
end;
end;
15:
begin // Point
with LabelDisplay do
begin
if (Pos('.', Caption) > 0) and (Operation = 0) then
Exit;
if not Ok then
Caption := Caption '.'
else
Caption := '0,';
Point := True;
end;
end;
16:
begin // pi
with LabelDisplay do
begin
Caption := FloatToStr(Pi);
Point := True;
Ok := False;
end;
end;
17:
begin // inverse
with LabelInverse do
begin
Caption := 'Inverse';
Hint := 'Inverse function';
if Xy then
begin
Visible := False;
Xy := False;
with LabelDisplay do
Caption := FloatToStr(Numb1);
end;
Visible := not Visible;
end;
end;
18:
begin // Power Xy
with LabelDisplay do
begin
if (Caption = '0') and not Xy then
Exit;
if not Xy then
begin
with LabelInverse do
begin
Caption := 'Power';
Hint := 'Power function (X^Y)';
Visible := True;
end;
Xy := True;
Numb1 := StrToFloat(Caption);
Caption := '0';
end
else
begin
with LabelInverse do
Visible := False;
Xy := False;
Caption := FloatToStr(Numb1);
end;
Ok := False;
end;
end;
19:
begin // exponential E
with LabelDisplay do
begin
if Caption = '0' then
Caption := '';
if Operation = 0 then
begin
Caption := Caption ('E');
Numb1 := StrToFloat(Caption);
end
else
begin
Caption := ('E');
Numb2 := StrToFloat(Caption);
end;
end;
end;
20:
begin // sine and sine inverse
with LabelDisplay do
begin
if Caption = '' then
Exit;
I := StrToFloat(Caption);
with LabelInverse do
begin
if Visible and not Xy then
begin
if (I < -1) or (I > 1) then
Exit;
I := ArcSin(I) * 180 / Pi;
Visible := False;
end
else
I := Sin(I * Pi / 180);
end;
Caption := FloatToStr(I);
Ok := False;
end;
end;
21:
begin // cosine and cosine inverse
with LabelDisplay do
begin
if Caption = '' then
Exit;
I := StrToFloat(Caption);
with LabelInverse do
begin
if Visible and not Xy then
begin
if (I < -1) or (I > 1) then
Exit;
I := ArcCos(I) * 180 / Pi;
Visible := False;
end
else
I := Cos(I * Pi / 180);
end;
Caption := FloatToStr(I);
Ok := False;
end;
end;
22:
begin // tan and tan inverse
with LabelDisplay do
begin
if Caption = '' then
Exit;
I := StrToFloat(Caption);
with LabelInverse do
begin
if Visible and not Xy then
begin
I := ArcTan(I) * 180 / Pi;
Visible := False;
end
else
I := Tan(I * Pi / 180);
end;
Caption := FloatToStr(I);
Ok := False;
end;
end;
23:
begin // 1/x
with LabelDisplay do
begin
if Caption = '0' then
Exit;
I := StrToFloat(Caption);
I := (1 / I);
Caption := FloatToStr(I);
Ok := False;
end;
end;
24:
begin // root
with LabelDisplay do
begin
if (Caption = '0') then
Exit
else if (Copy(Caption, 1, 1) = '-') then
begin
MessageBox(Handle, 'Negative Numbers Are Not Allowed!', 'Negative Numbers', MB_Ok);
Exit;
end;
I := StrToFloat(Caption);
I := Sqrt(I);
Caption := FloatToStr(I);
end;
end;
25:
begin // x square
with LabelDisplay do
begin
if Caption = '0' then
Exit;
I := StrToFloat(Caption);
I := Sqr(I);
Caption := FloatToStr(I);
end;
end;
26:
begin // log and 10^(x)
with LabelDisplay do
begin
if Caption = '0' then
Exit;
I := StrToFloat(Caption);
with LabelInverse do
begin
if Visible then
begin
I := Power(10, I);
Visible := False;
end
else
I := Log10(I);
end;
Caption := FloatToStr(I);
Ok := False;
end;
end;
27:
begin // Ln and exp(x)
with LabelDisplay do
begin
if Caption = '0' then
Exit;
I := StrToFloat(Caption);
with LabelInverse do
begin
if Visible then
begin
I := Exp(I);
Visible := False;
end
else
I := Ln(I);
end;
Caption := FloatToStr(I);
Ok := False;
end;
end;
28:
begin // /-
with LabelDisplay do
begin
if Caption = '0' then
Exit;
if Copy(Caption, Length(Caption), 1) = 'E' then
Caption := Caption '-'
else if Copy(Caption, Length(Caption), 1) = '-' then
Copy(Caption, 1, Length(Caption) - 1)
else if Copy(Caption, 1, 1) = '-' then
Caption := Copy(Caption, 2, Length(Caption))
else
Caption := '-' Caption;
if Operation = 0 then
Numb1 := StrToFloat(Caption);
end;
end;
29:
begin // Mc
with LabelMemory do
begin
Mem1 := 0;
Visible := False;
end;
end;
30:
begin // MR
with LabelDisplay do
begin
Caption := FloatToStr(Mem1);
if Operation = 0 then
Numb1 := StrToFloat(Caption)
else
Numb2 := StrToFloat(Caption);
end;
end;
31:
begin // M
with LabelDisplay do
Mem1 := Mem1 StrToFloat(Caption);
with LabelMemory do
Visible := True;
end;
32:
begin // C
if Operation = 0 then
Numb1 := 0
else
Numb2 := 0;
with LabelDisplay do
Caption := '0';
end;
33:
begin // AC
Numb1 := 0;
Numb2 := 0;
StoreResult := 0;
Point := False;
Ok := False;
Operation := 0;
with LabelDisplay do
Caption := '0';
end;
34:
begin // Random number
Randomize;
I := (random(1000) 1) / 1000;
with LabelDisplay do
Caption := FloatToStr(I);
end;
35:
begin // Fictorial;
with LabelDisplay do
begin
if Pos(',', Caption) > 0 then
begin
Application.MessageBox('Real numbers are not allowed!', 'Error', MB_Ok MB_IconStop);
Ok := False;
Exit;
end;
if Caption = '0' then
begin
Caption := '1';
Exit;
end;
I := StrToFloat(Caption);
if I > 60 then
begin
Application.MessageBox('Large value!', 'Error', MB_Ok MB_IconStop);
Ok := False;
Exit;
end;
M := I;
repeat
M := M - 1;
I := I * M;
until M <= 1;
Caption := FloatToStr(I);
end;
end;
36:
begin // Integer value
with LabelDisplay do
begin
I := StrToFloat(Caption);
I := Int(I);
Caption := FloatToStr(I);
Point := False;
Ok := False;
end;
end;
37:
begin // Rounded to the nearest integer
with LabelDisplay do
begin
I := StrToFloat(Caption);
I := Round(I);
Caption := FloatToStr(I);
Point := False;
Ok := False;
end;
end;
end; // case
end;

procedure TCalculatorForm.Img0MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
If not(ssLeft in Shift) then
Exit;
if (X <= 0) or (X >= (Sender as TImage).Width) or (Y <= 0) or (Y >= (Sender as TImage).Height) then
begin
TImage(Sender).Picture.Bitmap.LoadFromResourceID(HInstance, (TImage(Sender).Tag 1) * 10);
Pressed := False;
end
else
begin
TImage(Sender).Picture.Bitmap.LoadFromResourceID(HInstance, (TImage(Sender).Tag 1) * 10 1);
Pressed := True;
end;
end;

procedure TCalculatorForm.ImgMinimizeClick(Sender: TObject);
begin
Application.Minimize;
end;

procedure TCalculatorForm.ImgCloseClick(Sender: TObject);
begin
Application.Terminate;
end;

procedure TCalculatorForm.BgImgMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
Perform(Wm_SysCommand, $F012, 0);
end;

procedure TCalculatorForm.ImgAboutClick(Sender: TObject);
begin
AboutForm.ShowModal;
end;

function TCalculatorForm.GetNumb1: Extended;
begin
Result := FNumb1;
end;

procedure TCalculatorForm.SetNumb1(const Value: Extended);
begin
if FNumb1 <> Value then
FNumb1 := Value;
end;

function TCalculatorForm.GetNumb2: Extended;
begin
Result := FNumb2;
end;

procedure TCalculatorForm.SetNumb2(const Value: Extended);
begin
if FNumb2 <> Value then
FNumb2 := Value;
end;

function TCalculatorForm.GetOperation: Byte;
begin
Result := FOperation;
end;

procedure TCalculatorForm.SetOperation(const Value: Byte);
begin
if FOperation <> Value then
FOperation := Value;
end;

function TCalculatorForm.GetStoreResult: Extended;
begin
Result := FStoreResult;
end;

procedure TCalculatorForm.SetStoreResult(const Value: Extended);
begin
if FStoreResult <> Value then
FStoreResult := Value;
end;

function TCalculatorForm.GetMem1: Extended;
begin
Result := FMem1;
end;

procedure TCalculatorForm.SetMem1(const Value: Extended);
begin
if FMem1 <> Value then
FMem1 := Value;
end;

function TCalculatorForm.GetPoint: Boolean;
begin
Result := FPoint;
end;

procedure TCalculatorForm.SetPoint(const Value: Boolean);
begin
if FPoint <> Value then
FPoint := Value;
end;

function TCalculatorForm.GetOk: Boolean;
begin
Result := FOk;
end;

procedure TCalculatorForm.SetOk(const Value: Boolean);
begin
if FOk <> Value then
FOk := Value;
end;

function TCalculatorForm.GetXy: Boolean;
begin
Result := FXy;
end;

procedure TCalculatorForm.SetXy(const Value: Boolean);
begin
if FXy <> Value then
FXy := Value;
end;

function TCalculatorForm.GetPressed: Boolean;
begin
Result := FPressed;
end;

procedure TCalculatorForm.SetPressed(const Value: Boolean);
begin
if FPressed <> Value then
FPressed := Value;
end;

end.

===================引 用 stacker_liew 文 章===================
那個GetOperation和SetOperation跟我們一般設定最普通的property方法沒有什麼特別,所以應該問題不是在這裡。我只是不明白的是為什麼不是在呼叫Do_Operation(Sender)前先設定Operation,而是在Do_Operation(Sender)後再設定Operation這樣有意義嗎?
編輯記錄
stacker_liew 重新編輯於 2019-01-14 18:27:29, 註解 無‧
系統時間:2024-05-09 4:56:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!