Recent

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

deananorth

  • Newbie
  • Posts: 6
How do I check if an integer is assigned
« on: March 30, 2018, 03:12:35 pm »
I'm dealing with a problem trying to make sure an integer is assigned in an object using an if statement.

I tried two approaches that have fallen through.

First was to make sure it's not null (or 'nil' as pascal calls it).

So... if x <> nil then

But it doesn't compile. The compiler spits out Operator is not overloaded: "LongInt" = "Pointer"

So I google around and found out about the "Assigned" function.

So... if not assigned(x) then...

Again, the compiler's not having it and complains: "Error: Illegal parameter list"

So... How should I go about doing this?
Have I run into a compiler bug?

rvk

  • Hero Member
  • *****
  • Posts: 6940
Re: How do I check if an integer is assigned
« Reply #1 on: March 30, 2018, 03:17:50 pm »
Have I run into a compiler bug?
No, an Integer always has a value. There is no nil value for Integer.

The best choice you have is to assign an "invalid" value of -1, -maxint or any other value you deem invalid and check for that.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How do I check if an integer is assigned
« Reply #2 on: March 30, 2018, 03:36:13 pm »
Indeed, but I guess OP has a different problem and then he should post  example code.
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...

rvk

  • Hero Member
  • *****
  • Posts: 6940
Re: How do I check if an integer is assigned
« Reply #3 on: March 30, 2018, 03:42:08 pm »
Indeed, but I guess OP has a different problem and then he should post  example code.
Or She  :D

But you're correct. I read too fast over the "... complains: "Error: Illegal parameter list" part.

(S)he should indeed post some code.

jamie

  • Hero Member
  • *****
  • Posts: 7517
Re: How do I check if an integer is assigned
« Reply #4 on: March 30, 2018, 04:04:01 pm »
Would be nice to know what type of OBJECT is being referred to here?

Record
OBJECT
CLASSS

 They all have their use, personally I like OBJECT model for somethings when I need what looks like a record
with a inheritance feature.., non-dynamic.. I know classes can do it but this is with far less over head.

Anyway I think if a Class/OBJECT is being used here maybe  INIT method for the object/Constructor for dynamic o
just a Constructor for the Class.


 In any and all cases, you need an INITIATION method. With classes you do it in the Constructor.. etc.

 I know at one time the compiler would generate a zero block of memory when creating dynamic objects, not sure if
that still holds true?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How do I check if an integer is assigned
« Reply #5 on: March 30, 2018, 04:12:49 pm »
Programmer is male. Period. Even if their real name is the goddess of the hunt.
There are too many things taken away from men to allow such a thing to be common knowledge....

(I know a lot of good female programmers, btw)
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 #6 on: March 30, 2018, 04:14:48 pm »
First, the object class in question.

type
    person = class
    private
           x,y,vx,vy : integer;
    public
           constructor create; overload; {example of overleading No params}
           constructor create(args : array of integer); overload; {Has params i.e. the thing I want to check.}
           procedure sayhi; {Error occurs in this method}
    end;


Person is just a test class while I try OOP in freepascal/Lazarus.

procedure person.sayhi;
begin
     writeln('Hi from person.');
     if x <> nil then {<- throws Error: Operator is not overloaded: "LongInt" = "Pointer"}
     begin
        writeln(format('I am at %d, %d',[x,y])); { Sysutils FTW! :) }
     end;
end;


Also... *Reads through thread.* Me: Oh snap I think I've started a flame war- oh, wait I haven't - Have I?
« Last Edit: March 30, 2018, 04:21:09 pm by deananorth »

rvk

  • Hero Member
  • *****
  • Posts: 6940
Re: How do I check if an integer is assigned
« Reply #7 on: March 30, 2018, 04:23:48 pm »
And you say, if you comment out the if, that your program crashes ?

I made a complete example that works fine here:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses {$IFDEF UNIX} {$IFDEF UseCThreads}
  6.   cthreads, {$ENDIF} {$ENDIF}
  7.   Classes, SysUtils;
  8.  
  9. type
  10.   person = class
  11.   private
  12.     x, y, vx, vy: integer;
  13.   public
  14.     constructor Create; overload; {example of overleading No params}
  15.     constructor Create(args: array of integer); overload; {Has params i.e. the thing I want to check.}
  16.     procedure sayhi; {Error occurs in this method}
  17.   end;
  18.  
  19.   constructor person.Create;
  20.   begin
  21.     inherited Create;
  22.   end;
  23.  
  24.   constructor person.Create(args: array of integer); overload;
  25.   begin
  26.     inherited Create;
  27.   end;
  28.  
  29.   procedure person.sayhi;
  30.   begin
  31.     writeln('Hi from person.');
  32.     // if x <> nil then {<- throws Error: Operator is not overloaded: "LongInt" = "Pointer"}
  33.     begin
  34.       writeln(format('I am at %d, %d', [x, y])); { Sysutils FTW! :) }
  35.     end;
  36.   end;
  37.  
  38. var
  39.   P: person;
  40. begin
  41.   P := person.Create;
  42.   P.sayhi;
  43.   readln;
  44. end.

Result:
Code: [Select]
Hi from person.
I am at 0, 0

You mentioned a compiler error: "Error: Illegal parameter list"

Quote
Error: Illegal parameter list
You are calling a function with parameters that are of a different type than the declared parameters of the function.

So it must be something else in your code.

Please post some code which reproduces your problem.

jamie

  • Hero Member
  • *****
  • Posts: 7517
Re: How do I check if an integer is assigned
« Reply #8 on: March 30, 2018, 04:35:49 pm »
Integers do not have an unassigned value.

 you must come up with a different plan...

 when using NIL it is expected that you are referring to a POINTER type.

 So you have some choices here:

 1. Use -1 for the Integer as a not set value yet and then you can simply test for -1

 2. If -1 is needed then you can create another field of Boolean that informs you that your
 integer has a valid value in it..

  In your Class
 
    FIsValid:Boolean;

this needs to get set at some point in your code to tree..

so when you are test for "X" to be a valid value...

 if fIsValid then Use_X_For_SOMETHING...

 of course you need to set fIsValid at some point..

The only true wisdom is knowing you know nothing

rvk

  • Hero Member
  • *****
  • Posts: 6940
Re: How do I check if an integer is assigned
« Reply #9 on: March 30, 2018, 04:38:08 pm »
you must come up with a different plan...
Yes, that's what I thought too.

But OP is probably just making a simple coding error.
The result of the compiler error "Error: Illegal parameter list" is an indication of that.
We should get the real code which reproduces this error.

(It's not an "I want null for Integer" problem at all)

balazsszekely

  • Guest
Re: How do I check if an integer is assigned
« Reply #10 on: March 30, 2018, 05:22:03 pm »
I cannot comment on the "Illegal parameter list" error, but you can use variants and assign null if this is really necessary, like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   V: Variant;
  4.   I: Integer;
  5. begin
  6.   V := null;
  7.   if V = null then
  8.     ShowMessage('null'); //is this what are you after?
  9.   V := 3;
  10.   ShowMessage(v);
  11.   I := V;   //assign variant to an integer
  12.   ShowMessage(IntToStr(I));
  13. end;
« Last Edit: March 30, 2018, 05:24:25 pm by GetMem »

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How do I check if an integer is assigned
« Reply #11 on: March 30, 2018, 05:32:03 pm »
, but you can use variants and assign null if this is really necessary,
Oh, well...
Plz explain variants are evil.....
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 #12 on: March 30, 2018, 05:46:47 pm »
Quote
You mentioned a compiler error: "Error: Illegal parameter list"

When I check for assignment using "assigned(x)" instead of if "x <> nil", the illegal parameter list error showed up.

Code: Pascal  [Select][+][-]
  1. {A test procedure}
  2. procedure person.sayhi;
  3. begin
  4.      writeln('Hi from person.');
  5.      if not assigned(x) then { <- Error: Illegal parameter list. I suspect It doesn't like anything but pointers. }
  6.      begin
  7.         writeln(format('I am at %d, %d',[x,y]));
  8.      end;
  9. end;

Also it's good to know that when I declare variables in classes, they're just set to zero before I do anything with them.

Maybe  I should...:

Make a special "extended" boolean...

Code: Pascal  [Select][+][-]
  1. type
  2.     person = class
  3.     private
  4.            x,y,vx,vy : integer;
  5.            extended : boolean; {Added this to tell the class if I'm using one constructor or the other}
  6.     public
  7.            constructor create; overload;
  8.            constructor create(args : array of integer); overload; {set extended to true here and check for it}
  9.            procedure sayhi;
  10.     end;

...and change it depending on which constructor I used. (Overloading FTW.)

Code: Pascal  [Select][+][-]
  1. {Constructor with data passed in.}
  2. constructor person.create(args : array of integer);
  3. begin
  4.      x := args[0];
  5.      y := args[1];
  6.      vx := args[2];
  7.      vy := args[3];
  8.      writeln(format('Person created with (%d,%d,%d,%d)',[args[0],args[1],args[2],args[3]]));
  9.                  extended := true; {set extended here}
  10. end;

Code: Pascal  [Select][+][-]
  1. {A test procedure}
  2. procedure person.sayhi;
  3. begin
  4.      writeln('Hi from person.');
  5.      if extended then {check extended here.}
  6.      begin
  7.         writeln(format('I am at %d, %d',[x,y]));
  8.      end;
  9. end;

Sooooooo it's just me being a derp. I feel very stupid.

It kinda goes to show how helpful the freepascal/Lazarus community is. Thanks guys. :)

Edits: How many typing blunders and oversights do I have to make today?
« Last Edit: March 30, 2018, 06:38:14 pm by deananorth »

rvk

  • Hero Member
  • *****
  • Posts: 6940
Re: How do I check if an integer is assigned
« Reply #13 on: March 30, 2018, 05:51:47 pm »
Yes, Assigned() only accepts pointers.

In your last code you have vy := args[3]; etc.
You should check if you really provided an array with 4 elements.

Or create a special array-type with 4 elements so you can pass that.

Code: Pascal  [Select][+][-]
  1. type
  2.   ArrayForPerson = array[0..3] of Integer;
  3.  
  4. //...
  5.   constructor person.create(args: ArrayForPerson);
or
Code: Pascal  [Select][+][-]
  1. constructor person.create(args: array of integer);
  2. begin
  3.   if Length(args) = 4 then
  4.   begin
  5.     // assign the values
  6.   end;

deananorth

  • Newbie
  • Posts: 6
Re: How do I check if an integer is assigned
« Reply #14 on: March 30, 2018, 06:16:03 pm »
Quote
You should check if you really provided an array with 4 elements.

Nice catch.

My first option is using that if statement in the constructor, but I wanna see of using a special array also works.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {
  4.  A quick Object Oriented Programming Sanity test.
  5. }
  6.  
  7. uses sysutils;
  8.  
  9. type
  10.     person = class
  11.     private
  12.            x,y,vx,vy : integer;
  13.            hasattr : boolean;
  14.     public
  15.            constructor create; overload;
  16.            constructor create(args : array of integer); overload;
  17.            procedure sayhi;
  18.     end;
  19.  
  20. {
  21.  Plain "Vanilla" constructor.
  22.  It allows me to make the computer do something whenever I'm
  23.  creating an object.
  24. }
  25. constructor person.create;
  26. begin
  27.      writeln('Person created.');
  28.      extended := false; {set extended here}
  29. end;
  30.  
  31. {Constructor with data passed in.}
  32. constructor person.create(args : array of integer);
  33. begin
  34.      if length(args) = 4 then
  35.      begin
  36.        x := args[0];
  37.        y := args[1];
  38.        vx := args[2];
  39.        vy := args[3];
  40.        writeln(format('Person created with (%d,%d,%d,%d)',[args[0],args[1],args[2],args[3]]));
  41.        extended := true; {set extended here}
  42.      end
  43.      else
  44.      begin
  45.          writeln('The array I need to create this object needs 4 and only 4 elements.');
  46.      end;
  47. end;
  48.  
  49. {A test procedure}
  50. procedure person.sayhi;
  51. begin
  52.      writeln('Hi from person.');
  53.      if extended then {check extended here.}
  54.      begin
  55.         writeln(format('I am at %d, %d',[x,y]));
  56.      end;
  57.  
  58.      if (vx <> 0) or (vy <> 0) then
  59.      begin
  60.         writeln(format('I'' moving (%d, %d) units/tick',[x,y]));
  61.      end;
  62. end;
  63.  
  64. var
  65.    a: Person;
  66.    b: Person;
  67. begin
  68.      a := Person.create;
  69.      a.sayhi;
  70.      b := Person.create([1,2,3,4]);
  71.      b.sayhi;
  72.      readln();
  73. end.
  74.  

edit: One of my variable names turned out to be a reserved word. Whoops!
« Last Edit: March 30, 2018, 07:47:01 pm by deananorth »

 

TinyPortal © 2005-2018