Solved

Points for Evarest

Posted on 2004-08-29
6
169 Views
Last Modified: 2010-04-05
0
Comment
Question by:foxjax
  • 4
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
Evarest earned 500 total points
Comment Utility
Dear FoxJax,

indeed there where some errors in the code. It's been quite late yesterday when i posted my answer (too late to code in this window, i guess :-) This is the compiled code.

The errors can learn you something more, i think. As you're a beginning "Delphian", I'm quite happy to help you further. It's just because i like to get people to Delphi, as it's quite being crushed by other programming languages like C++(.NET), Java and C#... In my opinion, the last two have little to no advantages, and certaintly have some big disadvantages (like memory-management).

[BEGIN ERRORS]

[Error] MyObject.pas(38): Published property 'Name' cannot be of type ARRAY
[...]
[Error] MyObject.pas(41): Published property 'Limit' cannot be of type ARRAY
These errors come from the fact that you cannot place a RECORD or ARRAY (in this case) in the published section of the interface.
If you want, you can however place a CLASS in this area, but that's another story.
-> SOLUTION: copy these 4 properties to the public section.

[Error] MyObject.pas(39): Incompatible types
->Check whether the declaration of the GET and SET have the same type as the property:
Item[Index: integer]: integer -> GetItem(Index: integer): string;
Here, string must be changed to integer.

[Error] MyObject.pas(58): Undeclared identifier: 'FreeAndNil'
->Check whether SysUtils is in the uses clause at the top of your unit:
uses
  WinTypes, SysUtils, Classes;

[Error] MyObject.pas(87): Declaration of 'GetItem' differs from previous declaration
->This means that the declaration in the interface is different from the implementation.
function GetItem(Index: integer): integer;

[Error] MyObject.pas(89): Incompatible types: 'String' and 'Integer'
->Can be solved by correcting the last error.

[Error] MyObject.pas(103): Declaration of 'AddRecord' differs from previous declaration
->You must check againg whether the implementation and interface declaration is the same.
procedure AddRecord( Item: integer; const Name: string;
                     Limit: boolean; const Period: string );

After these changes, hit CTRL+F9 to compile.
HINT: When programming it's best to familiarize yourself with the numerous shortcuts. These will make it much easier and faster to check your code and to code in general...

(The following errors disappear after a compile.)
[Error] MyObject.pas(109): Undeclared identifier: 'Item'
[Error] MyObject.pas(110): Undeclared identifier: 'Limit'
[Error] MyObject.pas(111): Undeclared identifier: 'Period'
[Error] MyObject.pas(112): Undeclared identifier: 'fList'

This last error sticks:
[Error] MyObject.pas(118): Undeclared identifier: 'Q'
Just change Q into Index as Index is declared in the parameters.

This error always shows when there's an error in a certain unit.
[Fatal Error] Project2.dpr(8): Could not compile used unit 'MyObject.pas'

[END ERRORS]

And the corrected code

[BEGIN CODE]

unit MyObject;

interface

uses
  WinTypes, SysUtils, Classes;

type
  PCard = ^TCard;
  TCard = record
   Item: Integer;
   Name: String;
   Limit: Boolean;
   Period: String
  end;


  TMyObject = class
  private
    fList: TList;

    function GetName(Index: integer): string;
    procedure SetName(Index: integer; const Value: string);
    function GetItem(Index: integer): integer;
    procedure SetItem(Index: integer; Value: integer);
    function GetLimit(Index: integer): boolean;
    function GetPeriod(Index: integer): string;
    procedure SetLimit(Index: integer; const Value: boolean);
    procedure SetPeriod(Index: integer; const Value: string);
  public
    constructor Create;
    destructor Destroy; override;

    procedure AddRecord( Item: integer; const Name: string;
                         Limit: boolean; const Period: string );
    procedure DeleteRecord(Index: integer);
    procedure ClearRecords;

    property Name[Index: integer]: string read GetName write SetName;
    property Item[Index: integer]: integer read GetItem write SetItem;
    property Period[Index: integer]: string read GetPeriod write SetPeriod;
    property Limit[Index: integer]: boolean read GetLimit write SetLimit;
  published
  end;

implementation


{ TMyObject }

constructor TMyObject.Create;
begin
 inherited;
 fList :=TList.Create;
end;

destructor TMyObject.Destroy;
begin
 ClearRecords;
 FreeAndNil(fList);
 inherited;
end;

procedure TMyObject.SetName(Index: integer; const Value: string);
begin
 PCard(fList[Index])^.Name :=Value;
end;

procedure TMyObject.SetItem(Index: integer; Value: integer);
begin
 PCard(fList[Index])^.Item :=Value;
end;

procedure TMyObject.SetLimit(Index: integer; const Value: boolean);
begin
 PCard(fList[Index])^.Limit :=Value;
end;

procedure TMyObject.SetPeriod(Index: integer; const Value: string);
begin
 PCard(fList[Index])^.Period :=Value;
end;

function TMyObject.GetName(Index: integer): string;
begin
 result :=PCard(fList[Index])^.Name;
end;

function TMyObject.GetItem(Index: integer): integer;
begin
 result :=PCard(fList[Index])^.Item;
end;

function TMyObject.GetLimit(Index: integer): boolean;
begin
 result :=PCard(fList[Index])^.Limit;
end;

function TMyObject.GetPeriod(Index: integer): string;
begin
 result :=PCard(fList[Index])^.Period;
end;

procedure TMyObject.AddRecord( Item: integer; const Name: string;
                               Limit: boolean; const Period: string );
var
 MyRec: PCard; // thus a POINTER
begin
 new(MyRec); // reserve space for the record
 MyRec^.Name :=Name;
 MyRec^.Item :=Item;
 MyRec^.Limit :=Limit;
 MyRec^.Period :=Period;
 fList.Add(MyRec);
end;

procedure TMyObject.DeleteRecord( Index: integer );
begin
 if not (Index in [0..fList.Count-1]) then exit;
 Finalize(PCard(fList[Index])^);
 dispose(fList[Index]);
 fList.Delete(Index);
end;

procedure TMyObject.ClearRecords;
var
 Q: integer;
begin
 for Q :=fList.Count -1 downto 0 do
  begin
   DeleteRecord(Q);
  end;
end;

end.

[END CODE]

And an example on how to use this simple class:

[BEGIN EXAMPLE]

var
 MyObject: TMyObject;
begin
 MyObject :=TMyObject.Create;
 MyObject.AddRecord(1,'aName', true, 'aPeriod');
 MyObject.Period[0] :='myPeriod';
 MyObject.DeleteRecord(0);
 MyObject.Free;
end;

[END EXAMPLE]

That's it! If you have other questions, let me know...

And keep on Delphiing!

Evarest
0
 

Author Comment

by:foxjax
Comment Utility
Evarest

It works perfect!  Thanks for the extra info, it will now let me experiment and learn a lot more about Delphi.

150 points for you help

I agree with you about Delphi and other languages - i played with VB many years ago but i find Delhi to be far superior.

350 extra points and a Grade A for your attitude - keep it up!
0
 

Author Comment

by:foxjax
Comment Utility
Forgot to ask...

How would i edit a record or record field?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:foxjax
Comment Utility
Think i should clarify myself...

I know that something like MyObject.Period[<record number>] :=<New Value>; will update/edit the record field but i mean more along the line of:

MyObject.Edit<record number>(<data to edit record with>)
0
 
LVL 4

Expert Comment

by:Evarest
Comment Utility
Thanks for your most generous grades!...

Wrt your question: i quite don't understand the problem i think...

You can edit the record as follows (as you said in your post)

MyObject.Period[0] :='a_value';

However, your question is to edit it like:

MyObject.Edit0('a_value');

That's quite an odd way of editing something...

If you mean that you want to edit the entire record at once (thus name, period, ... with one procedure), that can be done as follows:

[BEGIN CODE]

procedure TMyObject.EditRecord( RecordIndex: integer;
                                                 Item: integer; const Name: string;
                                                 Limit: boolean; const Period: string );
begin
 PMyRec(fList[RecordIndex])^.Item :=Item;
 PMyRec(fList[RecordIndex])^.Name :=Name;
 PMyRec(fList[RecordIndex])^.Limit :=Limit;
 PMyRec(fList[RecordIndex])^.Period :=Period;
end;

[END CODE]

And call it like

EditRecord( 0, 2, 'a_Name', true, 'a_Period' );

If you want to set the record by inserting another record:

[BEGIN CODE]

// insert this line with the other properties (in the public section)
property Record[Index: integer]: TMyRec read GetRecord write SetRecord;

// these procedures go in the implementation section (don't forget to declare them also in the interface private section!)
function TMyObject.GetRecord( Index: integer ): TMyRec;
begin
 result :=PMyRec(fList[Index])^;
end;

procedure TMyObject.SetRecord( Index: integer;
                                                 const Rec: TMyRec );
begin
 PMyRec(fList[Index])^.Item :=Rec.Item;
 PMyRec(fList[Index])^.Name :=Rec.Name;
 PMyRec(fList[Index])^.Limit :=Rec.Limit;
 PMyRec(fList[Index])^.Period :=Rec.Period;
end;

[END CODE]

You can call it this way:

[BEGIN CODE]

MyObject.Record[0].Name :='a_Name';  // this I'm not entirely sure of, if you have problems with this, let me know

var
 MyRec: TMyRec;
begin
 MyRec.Name :='a_Name';
 MyRec.Item :=0;
 //...
 MyObject.Record[0] :=MyRec;
end;

[END CODE]

Please note that all this, i've written in the small "Experts" memobox (i don't have Delphi installed on the system i'm working on...). Mistakes are bound to happen in this box. Let me know if there are any problems, then I'll try to fix them!

Kind regards,
Evarest
0
 

Author Comment

by:foxjax
Comment Utility
Thanks Evarest, that has given me a lot to work with.

The EditRecord( 0, 2, 'a_Name', true, 'a_Period' ); method was what i meant but the MyObject.Record[0] :=MyRec;
 idea you presented is also a very interesting aspect for me to think on.

Overall, i have to thank you - your method of putting everything into a class has given me new live in delphi - i'm actually now finding myself spending more and more time learning it and "experimenting" with the class you presented.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now