PDA

View Full Version : A couple of more minor questions for FreePascal.



firehead
07-04-2010, 07:32 PM
1. Ok so in QuickBasic, the variable TIMER would return the number version(or double) of TIME type of variable.. Which would be very helpful.

In FreePascal, how can this be done without Lazarus?

2. Does FreePascal contain a "Lists" feature that C++ and C# have? It could be somewhat helpful.

Stoney
07-04-2010, 09:52 PM
1. If I understand you correctly, you want define an own type for your timer variable?
The simplest way would be:

type
TTime = Integer; // or Double...

...

var
Timer: TTime;

...

Timer := 60;
WriteLn(Format('Time = %d', [Timer]));


or if you want a more complex solution which also allows you set or get an integer or a double value for your timer:


type
Time = class
private
fTime: Double;
public
function getTime(): Double; Overload;
function getTime(): Integer; Overload;

procedure setTime(Value: Double); Overload;
procedure setTime(Value: Integer); Overload;
end;

...

var
Timer: TTime;

...

function TTime.getTime(): Double;
begin
Result := fTime;
end;

function TTime.getTime(): Integer;
begin
Result := Trunc(fTime);
end;

procedure TTime.setTime(Value: Double);
begin
fTime := Value;
end;

procedure TTime.setTime(Value: Integer);
begin
fTime := Double(Value);
end;

...

Timer := TTime.Create;
Timer.setTime(20);
WriteLn(IntToStr(Timer.getTime()));
Timer.setTime(30.0);
WriteLn(FloatToStr(Timer.getTime()));



2. Take a look at TList and this example: http://www.delphibasics.co.uk/RTL.asp?Name=TList
Works the same way if you use -Mobjfpc or -Mdelphi compiler switch. Since you are obviously using Lazarus, you don't need to worry about the compiler switch.