Recent

Author Topic: VAR RECORD doesn't work?  (Read 2076 times)

coradi

  • Full Member
  • ***
  • Posts: 163
VAR RECORD doesn't work?
« on: September 30, 2023, 10:15:07 pm »
Code: [Select]
VAR Person = Record
       Vorname : String[12];
end;

this example is in my Book for Turbo Pacscal 7.0.
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

coradi

  • Full Member
  • ***
  • Posts: 163
Re: VAR RECORD doesn't work?
« Reply #1 on: September 30, 2023, 10:26:08 pm »
The next example is Variante Records, this is the Version with Type
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

Kays

  • Hero Member
  • *****
  • Posts: 613
  • Whasup!?
    • KaiBurghardt.de
Re: VAR RECORD doesn't work?
« Reply #2 on: September 30, 2023, 11:02:45 pm »
The equal sign suggests this was supposed to be a type declaration, however var suggests a variable was meant to be declared (requiring a colon as the separator). Either way there is a mistake in your book. The code does correctly not work.
Yours Sincerely
Kai Burghardt

440bx

  • Hero Member
  • *****
  • Posts: 4727
Re: VAR RECORD doesn't work?
« Reply #3 on: October 01, 2023, 04:15:33 am »
The equal sign suggests this was supposed to be a type declaration, however var suggests a variable was meant to be declared (requiring a colon as the separator). Either way there is a mistake in your book. The code does correctly not work.
That makes sense.

@OP,

you should probably check online for an errata list for the book you're reading.  If there is one, it's quite likely there is an errata related to the construct you presented.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

coradi

  • Full Member
  • ***
  • Posts: 163
Re: VAR RECORD doesn't work?
« Reply #4 on: October 01, 2023, 10:05:29 am »
I found IT an TWO Books, both for tp7
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

wp

  • Hero Member
  • *****
  • Posts: 12457
Re: VAR RECORD doesn't work?
« Reply #5 on: October 01, 2023, 10:18:49 am »
I do have original BP7 on a VM with DOS, and when I try to compile a simple program with the declaration of the post the compiler jumps to the '=' and tells me: 'Error 86: ":" expected.'

Doing the same with FPC/Lazarus the error message even is a bit more explicit: 'project1.lpr(4,5) Error: Syntax error, ":" expected but "=" found'.

When you find that something "does not work", why don't you read the error message? Don't trust books. Even if the author wrote it correctly it does not necessarily mean that it will be printed like that. And if a second book tells you the same I would say that the second author took the first book as a "source of ideas" (or vice versa).
« Last Edit: October 01, 2023, 10:25:56 am by wp »

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: VAR RECORD doesn't work?
« Reply #6 on: October 01, 2023, 10:53:06 am »
I found IT an TWO Books, both for tp7
Which two books ? Please do share title, author, year of release/edition and in case possible ISBN.

The books that are leading on such topic are those from Borland (the official language guide for TP 7.0 for example).

The following attachments are for illustration and educational purpose only, (c)Borland.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

coradi

  • Full Member
  • ***
  • Posts: 163
Re: VAR RECORD doesn't work?
« Reply #7 on: October 01, 2023, 11:19:17 am »
OK, this works without TYPE
Code: [Select]
Program Test;

VAR Testrecord : RECORD
                Vorname : STRING[12];
                Nachname : STRING[12];
                end;
Begin
Testrecord.Vorname :='Tomi';
Writeln(Testrecord.Vorname);
end.
« Last Edit: October 01, 2023, 12:20:08 pm by coradi »
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

coradi

  • Full Member
  • ***
  • Posts: 163
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: VAR RECORD doesn't work?
« Reply #9 on: October 01, 2023, 11:39:59 am »
@coradi:
Note that the documentation for Free Pascal can be found here especially the "Language reference guide" might come in handy if you are new to Pascal.

Having said that, I do realize/acknowledge that the Free Pascal documentation is far from complete or logical ordered (not everything is explained into detail and there are a lot of features discussed that will probably overwhelm/confuse you as the (object)pascal language itself has improved over time).

When in doubt about the correctness of your book's examples you could try a somewhat older site such as tutorialspoint for example here about records to verify things (perhaps you are able to find such a site in your native language, that usually helps as well).

In case things are still not clear or you run into errors, then just post about it on the forums (either here or somewhere else) and try to show as much code as possible (even if the original example from your book is wrong -> in which case don't forget to mention that the example-code originates from a book to make clear that it is not your mistake). Many of the older books use example code that is tight to the underlying OS (usually MS-DOS) and not all examples work as expected anymore unless targeting the same platform (most people do not run MS-DOS anymore these days).

PS: Danke fur die links. The book really does it wrong. Weird.
« Last Edit: October 01, 2023, 11:43:07 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

coradi

  • Full Member
  • ***
  • Posts: 163
Re: VAR RECORD doesn't work?
« Reply #10 on: October 01, 2023, 12:17:48 pm »
Works in FPC and TP7
Ah ok, now I understand how it works without TYPE and with TYPED Record but Why his difference?. Why this two versions?
VAR VYZ : RECORD      is correct
TYPE XYZ = RECORD   is correct

My Example from above
https://forum.lazarus.freepascal.org/index.php/topic,64752.msg492772.html#msg492772


Where is the Benefit to use Type an not VSR?
I f I use TYPE I need another Line for Var XYZ : ZZZ; if I use VAR XYZ : RECORD, it's easier to use
« Last Edit: October 01, 2023, 12:24:31 pm by coradi »
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

rvk

  • Hero Member
  • *****
  • Posts: 6572
Re: VAR RECORD doesn't work?
« Reply #11 on: October 01, 2023, 12:22:11 pm »
I f I use TYPE I need another Line for Var XYZ : ZZZ; if I use VAR XYZ : RECORD, it's easier to use
And what if you want two variable of the same recordtype?  ;)

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: VAR RECORD doesn't work?
« Reply #12 on: October 01, 2023, 12:30:21 pm »
Ah ok, now I understand.
VAR VYZ : RECORD      is correct
TYPE XYZ = RECORD   is correct
Yups, you got it !  :)


Quote
Where is the Benefit to use Type an not VSR?
I f I use TYPE I need another Line for Var XYZ : ZZZ; if I use VAR XYZ : RECORD, it's easier to use
Pascal is a strong typed language (meaning that there is something named type compatibility, for an example regarding dynamic arrays and type compatibility see here) In practice that f.e. means that when you declare f.e. 2 records using exactly the same record fields that those record are not assignment compatible. Other than that if you need multiple variables using exactly the same record fields you need to declare them again for each variable. Declaring such a record as a specific type is faster in such cases.

Code: Pascal  [Select][+][-]
  1. var
  2.   firstrecord : record
  3.     field1: string;
  4.     field2: integer;
  5.     field3: char;
  6.   end;
  7.  
  8.   secondrecord : record
  9.     field1: string;
  10.     field2: integer;
  11.     field3: char;
  12.   end;
  13.  
  14.   thirdrecord : record
  15.     field1: string;
  16.     field2: integer;
  17.     field3: char;
  18.   end;
  19.  

vs

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRecord = record
  3.     field1: string;
  4.     field2: integer;
  5.     field3: char;
  6.   end;
  7.  
  8. var
  9.   firstrecord,
  10.   secondrecord,
  11.   thirdrecord : TMyRecordType;
  12.  

Regarding type compatibility, that is probably going to be explained when you create your own procedures and functions that accepts parameters of a certain type.

« Last Edit: October 01, 2023, 12:32:21 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

 

TinyPortal © 2005-2018