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.
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.
In which language?
ASKER
delphi, sorry I`ve missed that I wasn`t in the right category.
So I want it in Delphi.
So I want it in Delphi.
you can use a TList for this
ASKER
could u tell me how pls ?
ASKER
give me a simple example.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yep, a fine solution, mike
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 !
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;
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
Result := Count
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!
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]).nam e := TBook(MyClass[fromBook]).N ame;
TBook(MyClass[toBook]).pag es := TBook(MyClass[fromBook]).p ages;
end;
end;
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]).nam
TBook(MyClass[toBook]).pag
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;
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;
ASKER
no, cuz the result isn`t pointer ...
ASKER
I need a way to return the book. something like Books.Items[i] should return a TBook, right ?
ASKER
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;
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 ;-)
<--------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 ;-)
> result := p^; //you may return the result
meikl ;-)
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...
thanks a lot! I`ve solved the prob .... it`s just my code is a mess and I`m trying to make it clean...