Recent

Author Topic: the initial method  (Read 714 times)

cousinp

  • New Member
  • *
  • Posts: 20
the initial method
« on: September 08, 2024, 02:20:01 am »
What is the 'initial' method. See the code section attached. The book shows these methods but does not explain them. Is it a method, is it necessary, what does it do.
  TDayInWords=class
  sun: string;
  mon: string;
  tue: string;
  wed: string;
  thu: string;
  fri: string;
  sat: string;

procedure initial;
procedure findDayInWords(i:integer; sw:string);
end;

procedure TDayInWords.initial;
begin
  sun:='Sunday';
  mon:='Monday';
  tue:='Tuesday';
  wed:='Wednesday';

jamie

  • Hero Member
  • *****
  • Posts: 6694
Re: the initial method
« Reply #1 on: September 08, 2024, 03:53:17 am »
Code: Pascal  [Select][+][-]
  1.   TDayInWords=class
  2.   sun: string;
  3.   mon: string;
  4.   tue: string;
  5.   wed: string;
  6.   thu: string;
  7.   fri: string;
  8.   sat: string;
  9.  
  10. procedure initial;
  11. procedure findDayInWords(i:integer; sw:string);
  12. end;
  13.  
  14. procedure TDayInWords.initial;
  15. begin
  16.   sun:='Sunday';
  17.   mon:='Monday';
  18.   tue:='Tuesday';
  19.   wed:='Wednesday';
  20.  

Please use code tags, makes much easier on the eyes

Anyways. initial method would be that you much call to define all of your inner members at some point.
otherwise, all will be empty when you access them.

When Creating a class, all data members of the field are empty. So you need to define them (initialize) at some point to make them useable

for example:

 SUN :=' Day of rest';

 Wed := 'Hump Day' ;

etc/
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1417
    • Lebeau Software
Re: the initial method
« Reply #2 on: September 08, 2024, 04:49:46 am »
What is the 'initial' method. See the code section attached. The book shows these methods but does not explain them. Is it a method, is it necessary, what does it do.

It is a method, but it is acting similar to a constructor, in that it is initializing the record's data members with values.

Records do support actual constructors, though they cannot be parameter-less, as this method is.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

cousinp

  • New Member
  • *
  • Posts: 20
Re: the initial method
« Reply #3 on: September 08, 2024, 06:02:43 am »
Jamie sorry about no code tags, if I knew how to use them I would, I'm just starting and trying as hard as I can to get a grip on some foundational things. I appreciate your help and advice.
So to make sure I understand, are you saying without the '.initial'    sun:='day of rest';   
 sun would not have taken the value?

Remy, you say:  'it is initializing the record's data members with values'    are you referencing 
 procedure TDayInWords.initial;      as a record?   So that I can understand, procedures are more a record than an object.

Also you reference a parameter-less record,is it this one    procedure initial;
I don't see that procedure called anywhere in the program.

What is the link between   procedure initial;      and      procedure TDayInWords.initial;

Thanks everyone. Very much appreciative of all the help.

cousinp

  • New Member
  • *
  • Posts: 20
Re: the initial method
« Reply #4 on: September 08, 2024, 06:15:11 am »
I made a mis-statement about the      procedure initial;     not showing up in the program.
At the bottom of the program:

var
  DayInWords1:TDayInWords;
begin
  DayInWords1:=TDayInWords.create;
  DayInWords1.initial;
  DayInWords1.findDayInWords(strtoInt (ParamStr(1)),  ParamStr(2));

  DayInWords1.Free;

end.

Any comments on it's functioning?
Sorry and thanks again.

Lansdowne

  • New Member
  • *
  • Posts: 35
Re: the initial method
« Reply #5 on: September 08, 2024, 11:10:24 am »
Hi Cousinp.

When someone asks you to "use code tags", that is to do with posting your code on this Forum.  So when typing, find the dropdown marked Code, select Pascal, and paste your code between the [ code=pascal] and [ /code].  Perhaps you worked that one out.

You said "I'm just starting and trying to get a grip on foundational things".  And hopefully you understand that Classes and Objects are much more advanced/complex than what I would call foundational.

Have you copied that DayOfWeek example from the book, or is it your own idea?  Because there are many ways to do that sort of work using only simple Pascal constructs, and the Class you posted does not seem to be written as I would expect to find in a programming book.

dseligo

  • Hero Member
  • *****
  • Posts: 1395
Re: the initial method
« Reply #6 on: September 08, 2024, 12:17:10 pm »
Jamie sorry about no code tags, if I knew how to use them I would, I'm just starting and trying as hard as I can to get a grip on some foundational things. I appreciate your help and advice.
So to make sure I understand, are you saying without the '.initial'    sun:='day of rest';
 sun would not have taken the value?

It wouldn't.

Quote
Remy, you say:  'it is initializing the record's data members with values'    are you referencing
 procedure TDayInWords.initial;      as a record?   So that I can understand, procedures are more a record than an object.

This was probably lapsus by Remy, he surely meant 'classes'. Although, there are advanced records, which has similarities with clases.

Quote
What is the link between   procedure initial;      and      procedure TDayInWords.initial;

Former is declaration of type, second one is implementation.

var
  DayInWords1:TDayInWords; // here you declare variable with type TDayInWords
begin
  DayInWords1:=TDayInWords.create; // here you create new instance of your class (https://www.freepascal.org/docs-html/ref/refse38.html#x75-990006.4)
  DayInWords1.initial; // call to method initial
  DayInWords1.findDayInWords(strtoInt (ParamStr(1)),  ParamStr(2)); // call to method findDayInWords
  DayInWords1.Free; // you free your instance here
end.

Any comments on it's functioning?

I commented each line.

You could also have constructors in your class. They are called when you create class.
Below is simple example. I also added example method you could have to change language of days.

Code: Pascal  [Select][+][-]
  1. program simpleclass;
  2.  
  3. type
  4.  
  5.   { TDayInWords }
  6.  
  7.   TDayInWords = class
  8.     sun: string;
  9.     mon: string;
  10.     tue: string;
  11.     wed: string;
  12.     thu: string;
  13.     fri: string;
  14.     sat: string;
  15.     constructor Create;
  16.     procedure LanguageToCroatian;
  17.   end;
  18.  
  19. var
  20.   DayInWords1: TDayInWords;
  21.  
  22. { TDayInWords }
  23.  
  24. constructor TDayInWords.Create;
  25. begin
  26.   sun := 'Sunday';
  27.   mon := 'Monday';
  28.   tue := 'Tuesday';
  29.   wed := 'Wednesday';
  30.   thu := 'Thursday';
  31.   fri := 'Friday';
  32.   sat := 'Saturday';
  33. end;
  34.  
  35. procedure TDayInWords.LanguageToCroatian;
  36. begin
  37.   sun := 'Nedjelja';
  38.   mon := 'Ponedjeljak';
  39.   tue := 'Utorak';
  40.   wed := 'Srijeda';
  41.   thu := 'Četvrtak';
  42.   fri := 'Petak';
  43.   sat := 'Subota';
  44. end;
  45.  
  46. begin
  47.   DayInWords1 := TDayInWords.create;
  48.   WriteLn('Monday value after create: ', DayInWords1.mon);
  49.  
  50.   DayInWords1.LanguageToCroatian;
  51.   WriteLn('Monday value after change language: ', DayInWords1.mon);
  52.  
  53.   DayInWords1.Free;
  54. end.

cousinp

  • New Member
  • *
  • Posts: 20
Re: the initial method
« Reply #7 on: September 08, 2024, 08:15:19 pm »
Lansdowne,
Thank you for the "code tags" explanation.
Re: foundational things.  The book gives simple beginning programs, uses new words which are fields, methods, all kinds of things with purposes, if I can't understand the simpler programs, what hope do I have of understanding the coming more complex programs, how would I be able to make sense of anything. I agree they are more complex, but the book could give an explanation of what they are doing.

The DayOfWeek example is from the book: "Getting Started with Lazarus and Free Pascal" by Menkaura Abiola-Ellison, pg56.

Dseligo,
Thank you for your extensive //comments and examples.

"I will endeavor to persevere"- a quote from the movie - The Outlaw Josie Wales.
Thanks to all,
Paul

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1417
    • Lebeau Software
Re: the initial method
« Reply #8 on: September 09, 2024, 01:44:56 am »
Quote
Remy, you say:  'it is initializing the record's data members with values'    are you referencing
 procedure TDayInWords.initial;      as a record?   So that I can understand, procedures are more a record than an object.

This was probably lapsus by Remy, he surely meant 'classes'.

Agreed. Somehow I misread the TDayInWords type in question as being a record instead of a class. How odd of me...

In which case, the call to initial() in the following code makes no sense:

Code: Pascal  [Select][+][-]
  1. DayInWords1:=TDayInWords.create;
  2. DayInWords1.initial;
  3. DayInWords1.findDayInWords(strtoInt (ParamStr(1)),  ParamStr(2));
  4. DayInWords1.Free;
  5.  

The logic inside of initial() should be inside of create() instead.  Unless initial() is being offered as a way to reset an existing object to its default state after doing some work with it, in which case create() should call initial() so the user doesn't have to call it the 1st time.
« Last Edit: September 09, 2024, 01:47:36 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018