Lazarus

Free Pascal => Beginners => Topic started by: JLWest on January 07, 2019, 01:36:17 pm

Title: Record definations and assigning values. Don't understand
Post by: JLWest 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.
Title: Re: Record definations and assigning values. Don't understand
Post by: Bart 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
Title: Re: Record definations and assigning values. Don't understand
Post by: JLWest 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.
Title: Re: Record definations and assigning values. Don't understand
Post by: ccrause 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:-)
Title: Re: Record definations and assigning values. Don't understand
Post by: Bart 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
Title: Re: Record definations and assigning values. Don't understand
Post by: vangli on January 07, 2019, 02:11:25 pm
Hi

Guess your var definition should be:
Quote
LDRCD  : LoadRecord;
Title: Re: Record definations and assigning values. Don't understand
Post by: JLWest 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.
Title: Re: Record definations and assigning values. Don't understand
Post by: Bart 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 (https://en.wikipedia.org/wiki/The_Curious_Case_of_Benjamin_Button_(film))?

Bart
Title: Re: Record definations and assigning values. Don't understand
Post by: jamie 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.
Title: Re: Record definations and assigning values. Don't understand
Post by: JLWest 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.
Title: Re: Record definations and assigning values. Don't understand
Post by: lucamar 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 :-[
Title: Re: Record definations and assigning values. Don't understand
Post by: creaothceann 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.
TinyPortal © 2005-2018