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.
Delphi

Avatar of undefined
Last Comment
crystyan

8/22/2022 - Mon
InteractiveMind

In which language?
crystyan

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

So I want it in Delphi.
kretzschmar

you can use a TList for this
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
crystyan

ASKER
could u tell me how pls ?
crystyan

ASKER
give me a simple example.
SOLUTION
kretzschmar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kretzschmar

yep, a fine solution, mike
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
crystyan

ASKER
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 !
Mike Littlewood

type
  TMyClass .....
  private
    function GetTotalBooks: Integer;
  public
    property TotalBooks: Integer read GetTotalBooks;


function TMyClass.GetTotalBooks: Integer;
begin
  Result := FMyClass.Count
end;
Mike Littlewood

Sorry that should be

Result := Count
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
crystyan

ASKER
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!
Mike Littlewood

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;
Mike Littlewood

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;
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
crystyan

ASKER
no, cuz the result isn`t pointer ...
crystyan

ASKER
I need a way to return the book. something like Books.Items[i] should return a TBook, right ?
crystyan

ASKER
BookList.Items[i] I meant
Your help has saved me hundreds of hours of internet surfing.
fblack61
Mike Littlewood

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;
kretzschmar

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 ;-)
kretzschmar

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

meikl ;-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
crystyan

ASKER
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...