Recent

Author Topic: Record definations and assigning values. Don't understand  (Read 991 times)

JLWest

  • Sr. Member
  • ****
  • Posts: 396
Record definations and assigning values. Don't understand
« on: January 07, 2019, 01:36:17 pm »
 
Code: Pascal  [Select]
  1. type
  2.    LoadState =(LoadStart, LoadIdle, LoadStop);
  3.    LoadRecord = record
  4.                  AP        : string;
  5.                  Match    : string;
  6.                  LDState : LoadState;
  7.                 end;              
  8.  
  9. procedure TForm1.btnCutAirportClick(Sender: TObject);
  10.  Var
  11.   LDRCD      : LoadState;  
  12.  
  13. Begin
  14.    LDRCD.AP := 'KLAX';
  15.  //LDRCD.AP := Copy2Space(AIRPORT);
  16.    LDRCD.LDState := LoadIdle;        
  17.    

Error:
unit1.pas(224,10) Error: identifier idents no member "AP"

AP is defined as a string in the LoadRecord = Record type.
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

Bart

  • Hero Member
  • *****
  • Posts: 3364
    • Bart en Mariska's Webstek
Re: Record definations and assigning values. Don't understand
« Reply #1 on: January 07, 2019, 01:43:38 pm »
LDRCD  must be of type LoadRecord  O:-)

Not related, but it is good practice to start type-names with a T so
Code: Pascal  [Select]
  1. type
  2.    TLoadState =(LoadStart, LoadIdle, LoadStop);
  3.    TLoadRecord = record
  4.                  AP        : string;
  5.                  Match    : string;
  6.                  LDState : LoadState;
  7.                 end;

Then you can have:
Code: Pascal  [Select]
  1. var
  2.   LoadSate: TLoadSate;
  3.   LoadRecord: TLoadRecord

Bart

JLWest

  • Sr. Member
  • ****
  • Posts: 396
Re: Record definations and assigning values. Don't understand
« Reply #2 on: January 07, 2019, 01:56:43 pm »
ok but why can't I assign values? I can call them anything but if I can't assign values there are worthless.
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

ccrause

  • Full Member
  • ***
  • Posts: 136
Re: Record definations and assigning values. Don't understand
« Reply #3 on: January 07, 2019, 02:10:58 pm »
ok but why can't I assign values? I can call them anything but if I can't assign values there are worthless.

Bart already mentioned the problem with your code snippet:
LDRCD  must be of type LoadRecord  O:-)

Bart

  • Hero Member
  • *****
  • Posts: 3364
    • Bart en Mariska's Webstek
Re: Record definations and assigning values. Don't understand
« Reply #4 on: January 07, 2019, 02:11:11 pm »
ok but why can't I assign values? I can call them anything but if I can't assign values there are worthless.

Because you declared LDRCD to be of LoadState, where you meant it to be LoadRecord.

This piece of code compiles without errors (as one would expect):

Code: Pascal  [Select]
  1. type
  2.    TLoadState =(LoadStart, LoadIdle, LoadStop);
  3.    TLoadRecord = record
  4.                  AP        : string;
  5.                  Match    : string;
  6.                  LDState : TLoadState;
  7.                 end;
  8.  
  9. procedure foo;
  10.  Var
  11.   LoadRecord      : TLoadRecord;
  12.  
  13. Begin
  14.    LoadRecord.AP := 'KLAX';
  15.    LoadRecord.LDState := LoadIdle;
  16. end;

Bart

vangli

  • New member
  • *
  • Posts: 17
Re: Record definations and assigning values. Don't understand
« Reply #5 on: January 07, 2019, 02:11:25 pm »
Hi

Guess your var definition should be:
Quote
LDRCD  : LoadRecord;
Regards Bent

JLWest

  • Sr. Member
  • ****
  • Posts: 396
Re: Record definations and assigning values. Don't understand
« Reply #6 on: January 07, 2019, 02:21:15 pm »
Bart I'm sorry. I should have known something was wrong when I would type LRRCD. and the box comes up and never presented AP, Match, LDState as choices.

I guess that's why I post in beginner. Unfortunately at my age I'll always be  a beginner. It's fun trying to do this but can be frustrating at times.

 Thank you.
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

Bart

  • Hero Member
  • *****
  • Posts: 3364
    • Bart en Mariska's Webstek
Re: Record definations and assigning values. Don't understand
« Reply #7 on: January 07, 2019, 02:24:30 pm »
I guess that's why I post in beginner. Unfortunately at my age I'll always be  a beginner. It's fun trying to do this but can be frustrating at times.

So either you are young and do not age at all, or you age backwards?

Bart

jamie

  • Hero Member
  • *****
  • Posts: 1291
Re: Record definations and assigning values. Don't understand
« Reply #8 on: January 07, 2019, 11:09:22 pm »
I see you are getting into records now..

 If you have any plans  to store these records to file, you need to use a non-managed type of string.

 use short string type and set the length only what you need.

 AP :String[4];
 Match:String[6];
etc.
 That allocates space within the record to hold these strings.

JLWest

  • Sr. Member
  • ****
  • Posts: 396
Re: Record definations and assigning values. Don't understand
« Reply #9 on: January 07, 2019, 11:58:43 pm »
That's probably a good idea. I should go six on each. Most Airports Names are for but a few go to 8.

I didn't know you could do that. Allocate space on string within a record, But there is a lot I don't know.

Thanks.
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
500 GB SSD
3 Terabyte conventional disk space.

lucamar

  • Hero Member
  • *****
  • Posts: 1072
Re: Record definations and assigning values. Don't understand
« Reply #10 on: January 08, 2019, 01:02:31 am »
I didn't know you could do that. Allocate space on string within a record, But there is a lot I don't know.

As shown by jamie, that's done with ShortString type, the good ol' TP String. It's basically an array of char where the first byte "MyShortString[0]" stores the length of the string and the rest (up to the declared length or 255 if declared with just ShortString--or String in $H- state) contains the characters.

Ahh ... just in case you didn't know :-[
« Last Edit: January 08, 2019, 01:04:07 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3 (Home/Prof.), various DOSes.

creaothceann

  • Jr. Member
  • **
  • Posts: 96
Re: Record definations and assigning values. Don't understand
« Reply #11 on: January 08, 2019, 05:09:34 pm »
I should go six on each. Most Airports Names are for but a few go to 8.
Then use 8.