* * *

Author Topic: Class Help  (Read 1395 times)

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Class Help
« on: September 05, 2010, 04:23:21 pm »
This is my first real class and time using property's....
I think I coded it right but I went by what I knew and read from the tutorial in the Wiki..
My problem is when the code compiles it just closes(CLI) and I'm not sure why.. I made 2 units. One to create a file type and one to read it...
To read the document

Code: (Pascal) [Select]
{*Unit Used For opening and reading ZAX files...
ZAX Devolopment by Adam N.Andujar.*}
unit ZAX;
{*ZAX STATEMENTS
#ZAX to say its a ZAX file
%Author[Author Name here]
%About[About Document Here]
%BOD* Start the document
*EOD% Close the document
*}
{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,LCLProc;

Type
 TZAXHeader = Record//File header
 ZHead:String;
 ZAuthor:String;
 ZAbout:String;
 ZBody:String;
 end;
 TZaxFile = File Of TZAXHeader;//File Type
 TOpenZax = Class(TObject)
 Private
 Zax:TZaxheader;
 F:TZaxFile;
 FileN:String;
 Function GetAuthor:String;//Returns the Author
 Function GetBody:String;//Resturns the body
 Function GetAbout:String;//returns about
 Function GetIsDoc:Boolean;//Check for #ZAX
 Public
 Property IsDoc:Boolean Read GetIsDoc;//If the documents first line isnt #ZAX it wont be a ZAX doc though will still be read
 Property Body:String Read GetBody;//The whole file document
 Property Author:String Read GetAuthor;//Property to hold the author
 Property About:String Read GetAbout;//Property to hold about the document
 Constructor Create(const ZF:String);virtual;//Load an existing ZAX file
 end;

implementation
Function TOpenZax.GetIsDoc:Boolean;
begin
  IF(Zax.ZHead = '#ZAx')Then Result := True Else Result := False;
end;

Function TOpenZax.GetAbout:String;
begin
  Result := GetPart('[',']',ZAX.ZAbout);
end;

Function TOpenZax.GetAuthor:String;
begin
  Result := GetPart('[',']',Zax.ZAuthor);
end;

Function TopenZax.GetBody:String;
begin
  Result := GetPart('%BOD*','*EOD%',Zax.ZBody)
end;

Constructor TOpenZax.Create(const ZF:String);
begin
  FileN := ZF;
  AssignFile(F,FileN);
  Reset(F);
  Read(F,Zax);
end;

end.      
To create the document:
Code: (Pascal) [Select]
{*Unit used for creating ZAX files. Devolopment by Adam N.Andujar*}
unit ZAXCreate;

{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,LCLProc,ZAX;

Type
 TMakeZAX = object
 Private
 F:TZAXFile;
 NewZax:TZAXHeader;
 FileN:String;
 Function ReadAbout:String;//Read about
 Function ReadBody:String;//read the body
 Function ReadAuthor:String;//Read the author
 Procedure WriteBody(const S:String);virtual;// Add the body
 Procedure WriteAuthor(const S:String);virtual;//Add the author
 Procedure WriteAbout(const S:String);virtual;//Add about the document
 Public
 Property Body:String Read ReadBody Write WriteBody;
 Property About:String Read ReadAbout Write WriteAbout;
 Property Author:String Read ReadAuthor Write WriteAuthor;
 Constructor Create(const FN:String);//Set all the stuff :P
 Procedure MakeFile;//Create the file
 end;

implementation
Procedure TMakeZax.MakeFile;
begin
  Rewrite(F);
  Write(F,newZax);
  CloseFile(F);
end;

Function TMakeZAX.ReadAbout:String;
begin
  Result := About;
end;

Function TMakeZAX.ReadAuthor:String;
begin
  Result := Author;
end;

Function TmakeZAX.ReadBody:String;
begin
  Result := Body;
end;

Constructor TMakeZax.Create(const FN:String);
begin
  FileN := Fn;
  AssignFile(F,FileN);
  Author := '';
  About := '';
  Body := '';
end;

Procedure TMakeZAX.WriteAbout(const S:String);
begin
  About := S;
  NewZax.ZAbout := About;
end;

Procedure TMakeZAX.WriteBody(const S:String);
begin
  Body := S;
  NewZax.ZBody := Body;
end;

Procedure TMakeZAX.WriteAuthor(const S:String);
begin
  Author := S;
  NewZax.ZAuthor := Author;
end;

end.
                                        
The projects main program:
Code: (Pascal) [Select]
program project1;

{$mode objfpc}{$H+}

uses
  Classes,SysUtils,ZAX,ZAXCreate;

Var
 OpenZax:TOpenZAX;
 make:TMakeZax;

begin
  Make.Create('Z.zax');
  Make.Author := 'Adam';
  Make.About := 'Something';
  Make.Body := 'DJHADJAFK JAFNKJ AANFKJABRUHFAHNJVNKJFKJABKJN';
  Make.MakeFile;
  OPenZax.Create('Z.zax');
  Writeln(OPenZax.Author);
  Writeln('');
  Writeln('');
  Writeln(OpenZax.Body);
  Readln;
end.      
(I know the TMakeZax is still just an Object but I was gonna convert it when I fixed this problem)  :)
« Last Edit: September 05, 2010, 04:31:55 pm by captain jaster »
-Captain Jaster

Yogi

  • New member
  • *
  • Posts: 24
Re: Class Help
« Reply #1 on: September 05, 2010, 06:58:53 pm »
make:=TMakeZax.Create('Z.zax');

First initialize Your variable
Lazarus 0.9.31                   Debian 5.0.5 (lenny)
SVN 33021                        Linux 2.6.26-2-686
i386-linux-gtk2                    GNOME 2.22.3
FPC 2.7.1
SVN 19520

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: Class Help
« Reply #2 on: September 05, 2010, 07:04:50 pm »
make:=TMakeZax.Create('Z.zax');

First initialize Your variable
Thats what I though to..
But:
project1.pas(13,20) Error: Only static variables can be used in static methods or outside methods
project1.pas(13,20) Fatal: Syntax error, ";" expected but "identifier CREATE" found
So I came here..
EDIT I changed both to a class and did what you said.. The errors didnt come up but it still didnt work ;_;
« Last Edit: September 05, 2010, 07:11:10 pm by captain jaster »
-Captain Jaster

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: Class Help
« Reply #3 on: September 05, 2010, 07:44:11 pm »
Ok So I got it working..
But heres the thing.. When it compiles nothing happens...
It just shows the console.. and nothing else. I put a writeln('h') code and the h comes up but nothing else...
Code: (Pascal) [Select]
{*Unit used for creating ZAX files. Devolopment by Adam N.Andujar*}
unit ZAXCreate;

{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,LCLProc,ZAX;

Type
 TMakeZAX = Class(TObject)
 Private
 F:TZAXFile;
 NewZax:TZAXHeader;
 FileN:String;
 Function ReadAbout:String;//Read about
 Function ReadBody:String;//read the body
 Function ReadAuthor:String;//Read the author
 Procedure WriteBody(const S:String);virtual;// Add the body
 Procedure WriteAuthor(const S:String);virtual;//Add the author
 Procedure WriteAbout(const S:String);virtual;//Add about the document
 Public
 Property Body:String Read ReadBody Write WriteBody;
 Property About:String Read ReadAbout Write WriteAbout;
 Property Author:String Read ReadAuthor Write WriteAuthor;
 Constructor Create(const FN:String);//Set all the stuff :P
 Procedure MakeFile;//Create the file
 end;

implementation
Procedure TMakeZax.MakeFile;
begin
  AssignFile(F,FileN);
  Rewrite(F);
  Write(F,newZax);
  CloseFile(F);
end;

Function TMakeZAX.ReadAbout:String;
begin
  Result := About;
end;

Function TMakeZAX.ReadAuthor:String;
begin
  Result := Author;
end;

Function TmakeZAX.ReadBody:String;
begin
  Result := Body;
end;

Constructor TMakeZax.Create(const FN:String);
begin
  FileN := Fn;
  Author := '';
  About := '';
  Body := '';
end;

Procedure TMakeZAX.WriteAbout(const S:String);
begin
  About := '%About['+S+']';
  NewZax.ZAbout := About;
end;

Procedure TMakeZAX.WriteBody(const S:String);
begin
  Body := '%BOD*'+S+'*EOD%';
  NewZax.ZBody := Body;
end;

Procedure TMakeZAX.WriteAuthor(const S:String);
begin
  Author := '%Author['+S+']';
  NewZax.ZAuthor := Author;
end;

end.
                                     
Code: (Pascal) [Select]
{*Unit Used For opening and reading ZAX files...
ZAX Devolopment by Adam N.Andujar.*}
unit ZAX;
{*ZAX STATEMENTS
#ZAX to say its a ZAX file
%Author[Author Name here]
%About[About Document Here]
%BOD* Start the document
*EOD% Close the document
*}
{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,LCLProc;

Type
 TZAXHeader = Record//File header
 ZHead:String;
 ZAuthor:String;
 ZAbout:String;
 ZBody:String;
 end;
 TZaxFile = File Of TZAXHeader;//File Type
 TOpenZax = Class(TObject)
 Private
 Zax:TZaxheader;
 F:TZaxFile;
 FileN:String;
 Function GetAuthor:String;//Returns the Author
 Function GetBody:String;//Resturns the body
 Function GetAbout:String;//returns about
 Function GetIsDoc:Boolean;//Check for #ZAX
 Public
 Property IsDoc:Boolean Read GetIsDoc;//If the documents first line isnt #ZAX it wont be a ZAX doc though will still be read
 Property Body:String Read GetBody;//The whole file document
 Property Author:String Read GetAuthor;//Property to hold the author
 Property About:String Read GetAbout;//Property to hold about the document
 Constructor Create(const ZF:String);Virtual;//Load an existing ZAX file
 end;

implementation
Function TOpenZax.GetIsDoc:Boolean;
begin
  IF(Zax.ZHead = '#ZAx')Then Result := True Else Result := False;
end;

Function TOpenZax.GetAbout:String;
begin
  Result := GetPart('[',']',ZAX.ZAbout);
end;

Function TOpenZax.GetAuthor:String;
begin
  Result := GetPart('[',']',Zax.ZAuthor);
end;

Function TopenZax.GetBody:String;
begin
  Result := GetPart('%BOD*','*EOD%',Zax.ZBody)
end;

Constructor TOpenZax.Create(const ZF:String);
begin
  FileN := ZF;
  AssignFile(F,FileN);
  Reset(F);
  Read(F,Zax);
end;

end.                 


Code: (Pascal) [Select]
program project1;

{$mode objfpc}{$H+}

uses
  Classes,SysUtils,ZAX,ZAXCreate;

Var
 OpenZax:TOpenZAX;
 make:TMakeZax;

begin
  Writeln('h');
  Make := TMakeZAX.Create('Z.zax');
  Make.About := 'Just some book';
  Make.Author := 'Adam';
  Make.Body := 'HHHHHHHHHHHHHHHHHHHHHHHHHEnvjdn vkj noi3niovnwkvnknm3wlvk nkn3';
  Make.MakeFile;
  OpenZax := TOpenZax.Create('Z.zax');
  Writeln(OpenZax.Author);
  Writeln(OpenZax.Body);
  Readln;
end.
It doesn't do anything In the classes ._____.     
HELP!       
-Captain Jaster

Yogi

  • New member
  • *
  • Posts: 24
Re: Class Help
« Reply #4 on: September 06, 2010, 03:19:55 pm »
Oh, it is doing too much in the classes; its going endless


Property About:String Read ReadAbout Write WriteAbout;

Procedure TMakeZAX.WriteAbout(const S:String);
begin
  About := '%About['+S+']';      //  this generates a further call to "WriteAbout"
  NewZax.ZAbout := About;
end;           

To write-access the property "about" You are using "WriteAbout", but inside You are doing another write access to the same property so "WriteAbout" is calling itself again and aagain; same in ReadAbout

Don't forget to close all opened files and to free your objects/classes when they are not in use any more.


Hope this helps
Lazarus 0.9.31                   Debian 5.0.5 (lenny)
SVN 33021                        Linux 2.6.26-2-686
i386-linux-gtk2                    GNOME 2.22.3
FPC 2.7.1
SVN 19520

captain jaster

  • Sr. Member
  • ****
  • Posts: 386
Re: Class Help
« Reply #5 on: September 06, 2010, 05:56:57 pm »
Thanks yogi!
It works :D
-Captain Jaster

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads