Forum > Beginners

the initial method

(1/2) > >>

cousinp:
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:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  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'; 
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/

Remy Lebeau:

--- Quote from: cousinp 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.

--- End quote ---

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.

cousinp:
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:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version