![]() |
Calculate Min Max of integer
Hello how could i calculate this ... i was fooling around but still can't get it working here is what i got so far.
Basicly all values are the same MIN MAX VALUE
Delphi-Quellcode:
unit Unit3;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm3 = class(TForm) Button1: TButton; Timer1: TTimer; Button2: TButton; procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end; type TMyInt = record FValue: Integer; FMin: Integer; FMax: Integer; FInit: Boolean; private procedure SetValue(const IntValue: Integer); public procedure Init; property Value: Integer read FValue write SetValue; property Min: Integer read FMin; property Max: Integer read FMax; end; var Form3: TForm3; implementation {$R *.dfm} procedure TMyInt.Init; begin FInit := False; end; procedure TMyInt.SetValue(const IntValue: Integer); begin if FValue <> IntValue then FValue := IntValue; if not FInit then begin FMax := FValue; FMin := FValue; FInit := True; end else begin if FValue > FMax then FMax := FValue; if FValue < FMin then FMin := FValue; end; end; // usage procedure TForm3.Timer1Timer(Sender: TObject); var temp: TMyInt; begin temp.Init; temp.Value:=Random(100); form3.Caption:='Value '+inttostr(Temp.Value)+' Min '+inttostr(temp.Min)+' Max '+inttostr(temp.Max); end; end. |
Re: Calculate Min Max of integer
Shouldn't this be TRUE ?
Delphi-Quellcode:
procedure TMyInt.Init;
begin FInit := True; end; |
Re: Calculate Min Max of integer
Well result is the same i believe :roll:
But is there a simplier way for this too much code IMHO.. |
Re: Calculate Min Max of integer
Zitat:
|
Re: Calculate Min Max of integer
Well maybe becouse the random number?
|
Re: Calculate Min Max of integer
Well, I see... You know the problem, why don't you fix that? Let me show you something:
Delphi-Quellcode:
See the difference? ;)
unit Unit3;
interface {...} var Form3: TForm3; temp: TMyInt; {...} procedure TMyInt.Init; begin FInit := True; end; {...} procedure TForm3.Timer1Timer(Sender: TObject); begin temp.Init; temp.Value:=Random(100); form3.Caption:='Value '+inttostr(Temp.Value)+' Min '+inttostr(temp.Min)+' Max '+inttostr(temp.Max); end; end. |
Re: Calculate Min Max of integer
Yes but is this why not work?
|
Re: Calculate Min Max of integer
Try it... Or what would you like to know? It's hard to understand your question...
|
Re: Calculate Min Max of integer
Or is there a more newbie friendly solution to this more simple code?
|
Re: Calculate Min Max of integer
Delphi-Quellcode:
type
TMyInt = record private FHasValue: Boolean; FMax: Integer; FMin: Integer; FValue: Integer; procedure SetValue(AValue: Integer); public procedure Clear; property HasValue: Boolean read FHasValue; property Max: Integer read FMax; property Min: Integer read FMin; property Value: Integer read FValue write SetValue; end; type TForm3 = class(TForm) ButtonStart: TButton; ButtonStop: TButton; Timer1: TTimer; procedure ButtonStartClick(Sender: TObject); procedure ButtonStopClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } FMyInit: TMyInt; procedure ShowMyInit; public { Public declarations } end; implementation {$R *.dfm} procedure TMyInt.Clear; begin FHasValue := False; FMax := 0; FMin := 0; FValue := 0; end; procedure TMyInt.SetValue(AValue: Integer); begin if FHasValue then begin if FValue <> AValue then begin FValue := AValue; if FMax < AValue then FMax := AValue; if FMin > AValue then FMin := AValue; end; end else begin FHasValue := True; FMax := AValue; FMin := AValue; FValue := AValue; end; end; // usage procedure TForm3.ButtonStartClick(Sender: TObject); begin FMyInt.Clear; ShowMyInit; Timer1.Enabled := True; end; procedure TForm3.ButtonStopClick(Sender: TObject); begin Timer1.Enabled := False; end; procedure TForm3.ShowMyInit; begin if FMyInt.HasValue then Caption := Format('Value %d Min %d Max %d', [FMyInt.Value, FMyInt.Min, FMyInt.Max]) else Caption := 'Value Min Max' end; procedure TForm3.Timer1Timer(Sender: TObject); begin FMyInit.Value := Random(100); ShowMyInit; end; |
Re: Calculate Min Max of integer
Delphi-Quellcode:
procedure TMyInt.SetValue(IntValue: Integer);
begin if not FInit then begin FValue := IntValue; FMax := FValue; FMin := FValue; FInit := True; end else if FValue <> IntValue then begin FValue := IntValue; if FValue > FMax then FMax := FValue; if FValue < FMin then FMin := FValue; end; end;
Delphi-Quellcode:
I do not understand what he wants her more.
procedure TMyInt.SetValue(IntValue: Integer);
begin FValue := IntValue; if not FInit then begin FMax := FValue; FMin := FValue; FInit := True; end else if FValue > FMax then FMax := FValue else if FValue < FMin then FMin := FValue; end; - the codes for TMyInt all work - only the position of the variable, and that everything is constantly new initializes what the problem
Delphi-Quellcode:
procedure TForm3.Timer1Timer(Sender: TObject);
var temp: TMyInt; << this is not local, but must be declared globally in the form begin temp.Init; << and it should not be constantly re-initialized |
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz