I need some help. I want to be able to type cast a record structure as a
TObject to allow me to add it as the object property of a TStringList.
eg.
type
TProductRecord = record
ProductCode : String;
Description : String;
QuantityIn : Double;
...
end;
var
ProductRecord : TProductRecord;
begin
MyList := TStringList.Create;
MyList.AddObject(IntToStr(ThisRecordNo), TObject(ProductRecord));
I get a ' (1170) Invalid typecast' error at the AddObject line when I try
and compile.
I know if I make the record a class instead it will work, but I don't want
it to be a class.
Any help gratefully received.
Mark Harby
FHM Consulting Limited
Nottingham. UK
>Hello All
>
>I need some help. I want to be able to type cast a record structure as a
>TObject to allow me to add it as the object property of a TStringList.
>
>eg.
>
>type
>TProductRecord = record
> ProductCode : String;
> Description : String;
> QuantityIn : Double;
> ...
> end;
>var
> ProductRecord : TProductRecord;
>begin
>
> MyList := TStringList.Create;
> MyList.AddObject(IntToStr(ThisRecordNo), TObject(ProductRecord));
Try this:
MyList.AddObject(IntToStr(ThisRecordNo), TObject(@ProductRecord));
It compiles, not sure if it will work, you ll have to try it.
>I get a ' (1170) Invalid typecast' error at the AddObject line when I try
>and compile.
>I know if I make the record a class instead it will work, but I don't want
>it to be a class.
>
>Any help gratefully received.
>
>
>Mark Harby
>FHM Consulting Limited
>Nottingham. UK
>
>
--
Skybuck presents free software for windows 95 and up:
Network Info, UDP Speed Test and UDP Multicast Test
http://www.mycgiserver.com/~skybuck
Ah well, really must read a good book on Delphi.
Mark Harby
Nottingham. UK
type
PProductRecord = ^TProductRecord;
TProductRecord = record
ProductCode : String;
Description : String;
QuantityIn : Double;
...
end;
var
ProductRecord : TProductRecord;
Test: String;
begin
MyList := TStringList.Create;
MyList.AddObject(IntToStr(ThisRecordNo), TObject(@ProductRecord));
Test := PProductRecord(MyList.Objects[ThisRecordNo])^.ProductCode;