Link to home
Avatar of crystyan
crystyan

asked on

Simple Class question

I want to have in a class an array like this:
type TBook = record
  name: String;
  pages: Byte;
end;

private
  A: Array of TBook;
 
let`s say I want the class to have the name TMyClass

I want to have these:
MyClass: TMyClass;

MyClass[1].BookName := 'bookname' or bookname := MyClass[1].BookName;
and I don`t want to have a maximum number of A.
Also I want MyClass.Add(book: TBook);

I want all the code pls.

Thanks.
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

In which language?
Avatar of crystyan
crystyan

ASKER

delphi, sorry I`ve missed that I wasn`t in the right category.

So I want it in Delphi.
Avatar of kretzschmar
you can use a TList for this
could u tell me how pls ?
give me a simple example.
SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
yep, a fine solution, mike
and how could I make a property like
    property  TotalItems: Word read BookList.Count;

or how could I access TList properties ?

I want something like Book[1] := Book[2] too.

Thanks a lot !
type
  TMyClass .....
  private
    function GetTotalBooks: Integer;
  public
    property TotalBooks: Integer read GetTotalBooks;


function TMyClass.GetTotalBooks: Integer;
begin
  Result := FMyClass.Count
end;
Sorry that should be

Result := Count
thanks, me being dumb.

but tell me about how could I do this (my last qestion):

function TWebsite.GetItem(Index: Word): TBook;
var
  p: pBook;
begin
  if (Index < Books.Count) and (Index > -1) then
  begin
    if assigned(Books[Index]) then
    begin
      new(p);
      p := Books.Items[Index];
      Result.Title := p^.Title;
      Result.Author := p^.Author;
      dispose(p);
    end;
  end;

if I put there dispose(p) I can`t access it right. If I don`t put dispose(p) I`ll alloc memory each time I read.
how could I access the content of Books[Index] without a new pointer ?

thanks in a million!
Not sure if you can just equate the examples like this

procedure TForm1.CopyBook(fromBook, toBook: Integer);
begin
  if (MyClass[fromBook] <> nil) and (MyClass[toBook] <> nil) then
  begin
    MyClass[toBook] := MyClass[fromBook]
  end;
end;

or if you have to change the values instead

procedure TForm1.CopyBook(fromBook, toBook: Integer);
begin
  if (MyClass[fromBook] <> nil) and (MyClass[toBook] <> nil) then
  begin
    TBook(MyClass[toBook]).name := TBook(MyClass[fromBook]).Name;
    TBook(MyClass[toBook]).pages := TBook(MyClass[fromBook]).pages;
  end;
end;
Shouldnt that be

function TWebsite.GetItem(Index: Word): TBook;
begin
  if (Index < Books.Count) and (Index > -1) then
  begin
    if assigned(Books[Index]) then
    begin
      Result := Books.Items[Index];
    end;
  end;
no, cuz the result isn`t pointer ...
I need a way to return the book. something like Books.Items[i] should return a TBook, right ?
BookList.Items[i] I meant
Why do you want to return a pointer to the book when you already have the book in Books.Items[Index]

O wait, is Books your object list? You need to typcast it first in my example for the book

function TWebsite.GetItem(Index: Word): TBook;
begin
  if ( Index < Books.Count) and ( Index > -1) then
  begin
    if assigned( Books[ Index]) then
    begin
      Result := TBook( Books.Items[ Index]);
    end;
  end;
seems to be a replacement, or?

<--------paste begin
function TWebsite.GetItem(Index: Word): TBook;
var
  p: pBook;
begin
  if (Index < Books.Count) and (Index > -1) then
  begin
    if assigned(Books[Index]) then
    begin
      new(p);
      p := Books.Items[Index];
      Result.Title := p^.Title;
      Result.Author := p^.Author;
      dispose(p);
    end;
  end;
>--------paste end

function TWebsite.GetItem(Index: Word): TBook;
var
  p: pBook;
begin
  if (Index < Books.Count) and (Index > -1) then
  begin
    if assigned(Books[Index]) then
    begin
      //new(p); //nothing new, just for replacement no new pointer
      p := Books.Items[Index];
      Result.Title := p^.Title;
      Result.Author := p^.Author;
      //dispose(p);  //no dispose, because you may hold the data and not free it yet
    end;
  end;
  result := p^;  //you may return the result
end;

why not use mike's sample, in this case you have no handling qith the ^-sign

meikl ;-)
oops, forget the line
>  result := p^;  //you may return the result

meikl ;-)
lol

thanks a lot! I`ve solved the prob .... it`s just my code is a mess and I`m trying to make it clean...