Recent

Author Topic: Class Help  (Read 6679 times)

captian jaster

  • Guest
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][+][-]
  1. {*Unit Used For opening and reading ZAX files...
  2. ZAX Devolopment by Adam N.Andujar.*}
  3. unit ZAX;
  4. {*ZAX STATEMENTS
  5. #ZAX to say its a ZAX file
  6. %Author[Author Name here]
  7. %About[About Document Here]
  8. %BOD* Start the document
  9. *EOD% Close the document
  10. *}
  11. {$mode objfpc}{$H+}
  12.  
  13. interface
  14.  
  15. uses
  16.   Classes,SysUtils,LCLProc;
  17.  
  18. Type
  19.  TZAXHeader = Record//File header
  20.  ZHead:String;
  21.  ZAuthor:String;
  22.  ZAbout:String;
  23.  ZBody:String;
  24.  end;
  25.  TZaxFile = File Of TZAXHeader;//File Type
  26.  TOpenZax = Class(TObject)
  27.  Private
  28.  Zax:TZaxheader;
  29.  F:TZaxFile;
  30.  FileN:String;
  31.  Function GetAuthor:String;//Returns the Author
  32.  Function GetBody:String;//Resturns the body
  33.  Function GetAbout:String;//returns about
  34.  Function GetIsDoc:Boolean;//Check for #ZAX
  35.  Public
  36.  Property IsDoc:Boolean Read GetIsDoc;//If the documents first line isnt #ZAX it wont be a ZAX doc though will still be read
  37.  Property Body:String Read GetBody;//The whole file document
  38.  Property Author:String Read GetAuthor;//Property to hold the author
  39.  Property About:String Read GetAbout;//Property to hold about the document
  40.  Constructor Create(const ZF:String);virtual;//Load an existing ZAX file
  41.  end;
  42.  
  43. implementation
  44. Function TOpenZax.GetIsDoc:Boolean;
  45. begin
  46.   IF(Zax.ZHead = '#ZAx')Then Result := True Else Result := False;
  47. end;
  48.  
  49. Function TOpenZax.GetAbout:String;
  50. begin
  51.   Result := GetPart('[',']',ZAX.ZAbout);
  52. end;
  53.  
  54. Function TOpenZax.GetAuthor:String;
  55. begin
  56.   Result := GetPart('[',']',Zax.ZAuthor);
  57. end;
  58.  
  59. Function TopenZax.GetBody:String;
  60. begin
  61.   Result := GetPart('%BOD*','*EOD%',Zax.ZBody)
  62. end;
  63.  
  64. Constructor TOpenZax.Create(const ZF:String);
  65. begin
  66.   FileN := ZF;
  67.   AssignFile(F,FileN);
  68.   Reset(F);
  69.   Read(F,Zax);
  70. end;
  71.  
  72. end.      
  73.  
To create the document:
Code: Pascal  [Select][+][-]
  1. {*Unit used for creating ZAX files. Devolopment by Adam N.Andujar*}
  2. unit ZAXCreate;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes,SysUtils,LCLProc,ZAX;
  10.  
  11. Type
  12.  TMakeZAX = object
  13.  Private
  14.  F:TZAXFile;
  15.  NewZax:TZAXHeader;
  16.  FileN:String;
  17.  Function ReadAbout:String;//Read about
  18.  Function ReadBody:String;//read the body
  19.  Function ReadAuthor:String;//Read the author
  20.  Procedure WriteBody(const S:String);virtual;// Add the body
  21.  Procedure WriteAuthor(const S:String);virtual;//Add the author
  22.  Procedure WriteAbout(const S:String);virtual;//Add about the document
  23.  Public
  24.  Property Body:String Read ReadBody Write WriteBody;
  25.  Property About:String Read ReadAbout Write WriteAbout;
  26.  Property Author:String Read ReadAuthor Write WriteAuthor;
  27.  Constructor Create(const FN:String);//Set all the stuff :P
  28.  Procedure MakeFile;//Create the file
  29.  end;
  30.  
  31. implementation
  32. Procedure TMakeZax.MakeFile;
  33. begin
  34.   Rewrite(F);
  35.   Write(F,newZax);
  36.   CloseFile(F);
  37. end;
  38.  
  39. Function TMakeZAX.ReadAbout:String;
  40. begin
  41.   Result := About;
  42. end;
  43.  
  44. Function TMakeZAX.ReadAuthor:String;
  45. begin
  46.   Result := Author;
  47. end;
  48.  
  49. Function TmakeZAX.ReadBody:String;
  50. begin
  51.   Result := Body;
  52. end;
  53.  
  54. Constructor TMakeZax.Create(const FN:String);
  55. begin
  56.   FileN := Fn;
  57.   AssignFile(F,FileN);
  58.   Author := '';
  59.   About := '';
  60.   Body := '';
  61. end;
  62.  
  63. Procedure TMakeZAX.WriteAbout(const S:String);
  64. begin
  65.   About := S;
  66.   NewZax.ZAbout := About;
  67. end;
  68.  
  69. Procedure TMakeZAX.WriteBody(const S:String);
  70. begin
  71.   Body := S;
  72.   NewZax.ZBody := Body;
  73. end;
  74.  
  75. Procedure TMakeZAX.WriteAuthor(const S:String);
  76. begin
  77.   Author := S;
  78.   NewZax.ZAuthor := Author;
  79. end;
  80.  
  81. end.
  82.                                        
  83.  
The projects main program:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,SysUtils,ZAX,ZAXCreate;
  7.  
  8. Var
  9.  OpenZax:TOpenZAX;
  10.  make:TMakeZax;
  11.  
  12. begin
  13.   Make.Create('Z.zax');
  14.   Make.Author := 'Adam';
  15.   Make.About := 'Something';
  16.   Make.Body := 'DJHADJAFK JAFNKJ AANFKJABRUHFAHNJVNKJFKJABKJN';
  17.   Make.MakeFile;
  18.   OPenZax.Create('Z.zax');
  19.   Writeln(OPenZax.Author);
  20.   Writeln('');
  21.   Writeln('');
  22.   Writeln(OpenZax.Body);
  23.   Readln;
  24. end.      
  25.  
(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 »

Yogi

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

First initialize Your variable
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

captian jaster

  • Guest
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 »

captian jaster

  • Guest
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][+][-]
  1. {*Unit used for creating ZAX files. Devolopment by Adam N.Andujar*}
  2. unit ZAXCreate;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes,SysUtils,LCLProc,ZAX;
  10.  
  11. Type
  12.  TMakeZAX = Class(TObject)
  13.  Private
  14.  F:TZAXFile;
  15.  NewZax:TZAXHeader;
  16.  FileN:String;
  17.  Function ReadAbout:String;//Read about
  18.  Function ReadBody:String;//read the body
  19.  Function ReadAuthor:String;//Read the author
  20.  Procedure WriteBody(const S:String);virtual;// Add the body
  21.  Procedure WriteAuthor(const S:String);virtual;//Add the author
  22.  Procedure WriteAbout(const S:String);virtual;//Add about the document
  23.  Public
  24.  Property Body:String Read ReadBody Write WriteBody;
  25.  Property About:String Read ReadAbout Write WriteAbout;
  26.  Property Author:String Read ReadAuthor Write WriteAuthor;
  27.  Constructor Create(const FN:String);//Set all the stuff :P
  28.  Procedure MakeFile;//Create the file
  29.  end;
  30.  
  31. implementation
  32. Procedure TMakeZax.MakeFile;
  33. begin
  34.   AssignFile(F,FileN);
  35.   Rewrite(F);
  36.   Write(F,newZax);
  37.   CloseFile(F);
  38. end;
  39.  
  40. Function TMakeZAX.ReadAbout:String;
  41. begin
  42.   Result := About;
  43. end;
  44.  
  45. Function TMakeZAX.ReadAuthor:String;
  46. begin
  47.   Result := Author;
  48. end;
  49.  
  50. Function TmakeZAX.ReadBody:String;
  51. begin
  52.   Result := Body;
  53. end;
  54.  
  55. Constructor TMakeZax.Create(const FN:String);
  56. begin
  57.   FileN := Fn;
  58.   Author := '';
  59.   About := '';
  60.   Body := '';
  61. end;
  62.  
  63. Procedure TMakeZAX.WriteAbout(const S:String);
  64. begin
  65.   About := '%About['+S+']';
  66.   NewZax.ZAbout := About;
  67. end;
  68.  
  69. Procedure TMakeZAX.WriteBody(const S:String);
  70. begin
  71.   Body := '%BOD*'+S+'*EOD%';
  72.   NewZax.ZBody := Body;
  73. end;
  74.  
  75. Procedure TMakeZAX.WriteAuthor(const S:String);
  76. begin
  77.   Author := '%Author['+S+']';
  78.   NewZax.ZAuthor := Author;
  79. end;
  80.  
  81. end.
  82.                                      
  83.  
Code: Pascal  [Select][+][-]
  1. {*Unit Used For opening and reading ZAX files...
  2. ZAX Devolopment by Adam N.Andujar.*}
  3. unit ZAX;
  4. {*ZAX STATEMENTS
  5. #ZAX to say its a ZAX file
  6. %Author[Author Name here]
  7. %About[About Document Here]
  8. %BOD* Start the document
  9. *EOD% Close the document
  10. *}
  11. {$mode objfpc}{$H+}
  12.  
  13. interface
  14.  
  15. uses
  16.   Classes,SysUtils,LCLProc;
  17.  
  18. Type
  19.  TZAXHeader = Record//File header
  20.  ZHead:String;
  21.  ZAuthor:String;
  22.  ZAbout:String;
  23.  ZBody:String;
  24.  end;
  25.  TZaxFile = File Of TZAXHeader;//File Type
  26.  TOpenZax = Class(TObject)
  27.  Private
  28.  Zax:TZaxheader;
  29.  F:TZaxFile;
  30.  FileN:String;
  31.  Function GetAuthor:String;//Returns the Author
  32.  Function GetBody:String;//Resturns the body
  33.  Function GetAbout:String;//returns about
  34.  Function GetIsDoc:Boolean;//Check for #ZAX
  35.  Public
  36.  Property IsDoc:Boolean Read GetIsDoc;//If the documents first line isnt #ZAX it wont be a ZAX doc though will still be read
  37.  Property Body:String Read GetBody;//The whole file document
  38.  Property Author:String Read GetAuthor;//Property to hold the author
  39.  Property About:String Read GetAbout;//Property to hold about the document
  40.  Constructor Create(const ZF:String);Virtual;//Load an existing ZAX file
  41.  end;
  42.  
  43. implementation
  44. Function TOpenZax.GetIsDoc:Boolean;
  45. begin
  46.   IF(Zax.ZHead = '#ZAx')Then Result := True Else Result := False;
  47. end;
  48.  
  49. Function TOpenZax.GetAbout:String;
  50. begin
  51.   Result := GetPart('[',']',ZAX.ZAbout);
  52. end;
  53.  
  54. Function TOpenZax.GetAuthor:String;
  55. begin
  56.   Result := GetPart('[',']',Zax.ZAuthor);
  57. end;
  58.  
  59. Function TopenZax.GetBody:String;
  60. begin
  61.   Result := GetPart('%BOD*','*EOD%',Zax.ZBody)
  62. end;
  63.  
  64. Constructor TOpenZax.Create(const ZF:String);
  65. begin
  66.   FileN := ZF;
  67.   AssignFile(F,FileN);
  68.   Reset(F);
  69.   Read(F,Zax);
  70. end;
  71.  
  72. end.                  
  73.  
  74.  

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,SysUtils,ZAX,ZAXCreate;
  7.  
  8. Var
  9.  OpenZax:TOpenZAX;
  10.  make:TMakeZax;
  11.  
  12. begin
  13.   Writeln('h');
  14.   Make := TMakeZAX.Create('Z.zax');
  15.   Make.About := 'Just some book';
  16.   Make.Author := 'Adam';
  17.   Make.Body := 'HHHHHHHHHHHHHHHHHHHHHHHHHEnvjdn vkj noi3niovnwkvnknm3wlvk nkn3';
  18.   Make.MakeFile;
  19.   OpenZax := TOpenZax.Create('Z.zax');
  20.   Writeln(OpenZax.Author);
  21.   Writeln(OpenZax.Body);
  22.   Readln;
  23. end.
  24.  
It doesn't do anything In the classes ._____.     
HELP!       

Yogi

  • New Member
  • *
  • Posts: 42
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 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

captian jaster

  • Guest
Re: Class Help
« Reply #5 on: September 06, 2010, 05:56:57 pm »
Thanks yogi!
It works :D

 

TinyPortal © 2005-2018