Recent

Author Topic: [SOLVED] Enumerated variables: increment and set  (Read 2945 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Enumerated variables: increment and set
« on: November 02, 2017, 01:43:46 pm »
hi All,
i just started to work with enumerated variables.
for the code below and attached i am trying to increment the value of the s variable and set the t variable one step backwards of s.
please help with how i could do this.
thank you

Code: Pascal  [Select][+][-]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6. uses
  7.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  8.  
  9. type
  10.   TStatus = (action_1, action_2, action_3, action_4, action_5);
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     Button1: TButton;
  15.     Memo1: TMemo;
  16.     procedure Button1Click(Sender: TObject);
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. procedure TForm1.Button1Click(Sender: TObject);
  27. var
  28.   s: TStatus;
  29.   t: TStatus;   //one action before action s
  30.   r: string;
  31.  
  32. begin
  33.   s:= action_4;   //how can i increment s to action 5?  how can i set t to action 4?
  34.   writestr(r, s);
  35.   Memo1.Append(r);
  36.   Memo1.Append(inttostr(ord(s)));
  37. end;
  38.  
  39. end.
« Last Edit: November 02, 2017, 03:01:37 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Enumerated variables: increment and set
« Reply #1 on: November 02, 2017, 01:58:53 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   s: TStatus;
  4.   t: TStatus;   //one action before action s
  5.   r: string;
  6. begin
  7.   s := action_4;
  8.   writestr(r, s);
  9.   Memo1.Append(Format('s=%s (%d)',[r, Ord(s)]));
  10.   //how can i increment s to action 5?  how can i set t to action 4?
  11.   t:=s;
  12.  
  13.   if s < High(TStatus) then  
  14.     s:=Succ(s);
  15.   writestr(r, s);
  16.   Memo1.Append(Format('after s:=Succ(s), s=%s (%d)',[r, Ord(s)]));
  17.  
  18.   writestr(r, t);
  19.   Memo1.Append(Format('after t:=s, t=%s (%d)',[r, Ord(t)]));
  20. end;
« Last Edit: November 02, 2017, 02:01:53 pm by howardpc »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Enumerated variables: increment and set
« Reply #2 on: November 02, 2017, 02:17:29 pm »
thank you!

without involving s, is there a way to just set t to value 3 (action_4)?
Lazarus 2.0.2 64b on Debian LXDE 10

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Enumerated variables: increment and set
« Reply #3 on: November 02, 2017, 03:01:21 pm »
Lazarus 2.0.2 64b on Debian LXDE 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Enumerated variables: increment and set
« Reply #4 on: November 02, 2017, 05:26:21 pm »
without involving s, is there a way to just set t to value 3 (action_4)?
either
Code: Pascal  [Select][+][-]
  1. t := action_4;
or
Code: Pascal  [Select][+][-]
  1. t := TStatus(3);

 

TinyPortal © 2005-2018