Recent

Author Topic: How do I check if an integer is assigned  (Read 14936 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How do I check if an integer is assigned
« Reply #15 on: March 30, 2018, 06:45:38 pm »
Oh yeah, very smart to use extended, which is a reserved word, in your code.
Code: Pascal  [Select][+][-]
  1. type
  2.     person = class
  3.     private
  4.            x,y,vx,vy : integer;
  5.            extended : boolean;
  6.  

That's worse than trolling. That is clueless. And you do that on purpose?
(I am rather limited to water guns since I live in a grown up territory)
« Last Edit: March 30, 2018, 06:53:33 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

deananorth

  • Newbie
  • Posts: 6
Re: How do I check if an integer is assigned
« Reply #16 on: March 30, 2018, 07:46:15 pm »
Ah, didn't realise "Extended" was a reserved word.

My mistake.

I'll quickly change that.

Code: Pascal  [Select][+][-]
  1. type
  2.     person = class
  3.     private
  4.            x,y,vx,vy : integer;
  5.            hasattr : boolean;
  6.     public
  7.            constructor create; overload;
  8.            constructor create(args : array of integer); overload;
  9.            procedure sayhi;
  10.     end;
  11.  

Aaaaaannnd that should be that.

I'm kinda new to pascal.

Also, out of curiosity. Why does "extended" highlight in Vim but not in Lazarus?

« Last Edit: March 30, 2018, 08:19:26 pm by deananorth »

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: How do I check if an integer is assigned
« Reply #17 on: March 30, 2018, 09:03:08 pm »
Oh yeah, very smart to use extended, which is a reserved word, in your code.
Uh, Thaddy, are you really sure extended is a reserved word ?????
https://www.freepascal.org/docs-html/ref/refse3.html

Extended words cannot be be redefined by the programmer and I'm sure extended can be redefined.

Also, extended isn't highlighted in the IDE, and all reserved words should be.

Extended is a type (like Integer and Double). So it is best not to use it anyway as a variable-name.

deananorth

  • Newbie
  • Posts: 6
Re: How do I check if an integer is assigned
« Reply #18 on: March 30, 2018, 09:04:54 pm »
Turns out Extended is a data type, and Lazarus doesn't seem to highlight data types.

From what I've read, it's a kind of floating point number that is a little longer (and thus more precise) than a double and is usually used on intel x86 processors.

Trying to use extended on other processors such as ARM tend to end up as a double with a different lick of paint.

Source: https://www.freepascal.org/docs-html/prog/progsu159.html
« Last Edit: March 30, 2018, 09:12:02 pm by deananorth »

totya

  • Hero Member
  • *****
  • Posts: 722
Re: How do I check if an integer is assigned
« Reply #19 on: March 30, 2018, 09:13:22 pm »
I don't understand clearly what is the problem, but I play with it... :p

Code: Pascal  [Select][+][-]
  1. unit mainform;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   unit_clevervalue;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.     x: TCleverValue;
  21.   public
  22.     procedure ShowValue(const cv: TCleverValue);
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.ShowValue(const cv: TCleverValue);
  36. var
  37.   s1, s2: string;
  38. begin
  39.   s1:=''; s2:='';
  40.  
  41.   if cv=nil
  42.     then s2:= '(nil)'
  43.     else s2:= '(not nil)';
  44.  
  45.   if cv<>nil then
  46.     if cv.GotValue
  47.       then s1:= '(got value)'
  48.       else s1:= '(untouched)';
  49.  
  50.   if cv<> nil
  51.     then Memo1.Lines.Add('x: '+s1+' , '+s2+' value: '+IntToStr(cv.Value))
  52.     else Memo1.Lines.Add('x: '+s2);
  53. end;
  54.  
  55. procedure TForm1.Button1Click(Sender: TObject);
  56. begin
  57.   Memo1.Clear;
  58.  
  59.   ShowValue(x);
  60.  
  61.   x:= nil;
  62.   x:= TCleverValue.Create;
  63.  
  64.   try
  65.     ShowValue(x);
  66.       x.Value:= 10;
  67.     ShowValue(x);
  68.       x.Value:= 200;
  69.     ShowValue(x);
  70.       x.Value:= 0;
  71.     ShowValue(x);
  72.       x.Value:= 300;
  73.     ShowValue(x);
  74.   finally
  75.     if Assigned(x) then FreeAndNil(x);
  76.   end;
  77.  
  78.   ShowValue(x);
  79. end;
  80.  
  81. end.
  82.  

Code: Pascal  [Select][+][-]
  1. unit unit_clevervalue;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.   TCleverValue = class
  12.     public
  13.       constructor Create;
  14.  
  15.     private
  16.       FValue : integer;
  17.     protected
  18.       procedure SetValue (const _Value: integer);
  19.     public
  20.       property
  21.         Value: integer read FValue write SetValue;
  22.  
  23.     private
  24.       FGotValue: boolean;
  25.     public
  26.       property GotValue : boolean read FGotValue;
  27.   end;
  28.  
  29.  
  30. implementation
  31.  
  32. constructor TCleverValue.Create;
  33. begin
  34.   FValue:= 0;
  35.   FGotValue:= false;
  36. end;
  37.  
  38. procedure TCleverValue.SetValue (const _Value: integer);
  39. begin
  40.   FValue:= _Value;
  41.   FGotValue:= true;
  42. end;
  43.  
  44. end.
  45.  

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: How do I check if an integer is assigned
« Reply #20 on: March 30, 2018, 09:15:58 pm »
I don't understand clearly what is the problem, but I play with it... :p
There is no problem.
Try reading beyond the first few messages in this topic.

totya

  • Hero Member
  • *****
  • Posts: 722
Re: How do I check if an integer is assigned
« Reply #21 on: March 30, 2018, 10:14:23 pm »
I don't understand clearly what is the problem, but I play with it... :p
There is no problem.
Try reading beyond the first few messages in this topic.

Okay, but I don't see any important things... The result is similar, he use additional boolean variable as I. In my code this boolean show this variable clean or not, he code show override is executed or not.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: How do I check if an integer is assigned
« Reply #22 on: March 30, 2018, 10:58:02 pm »
Okay, but I don't see any important things... The result is similar, he use additional boolean variable as I. In my code this boolean show this variable clean or not, he code show override is executed or not.
Then you could just as easily have used a variant.
You can just check if varType(x) is varInteger and you know if it has been assigned an integer.

But you're correct. TCleverValue is one of the multiple possible ways to mark if a variable is assigned or not.

 

TinyPortal © 2005-2018