لم تعُد "مجموعات Google" تتيح المشاركات أو الاشتراكات الجديدة من Usenet. وسيبقى بالإمكان عرض المحتوى السابق.

Type Casting a Record Structure as TObject.

2,197 مرّة مشاهدة
التخطي إلى أول رسالة غير مقروءة

Mark Harby

غير مقروءة،
12‏/11‏/2001، 11:28:00 ص12‏/11‏/2001
إلى
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));


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

غير مقروءة،
14‏/11‏/2001، 10:31:23 ص14‏/11‏/2001
إلى
On Mon, 12 Nov 2001 16:28:00 -0000, "Mark Harby"
<mark....@nospam.virgin.net> wrote:

>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

Mark Harby

غير مقروءة،
15‏/11‏/2001، 6:19:37 م15‏/11‏/2001
إلى
Thanks for replying, yes it does compile but it doesn't work (at least I
can't get it to work when trying to retrieve the data). I have therefore
relented and doubled-up on my user-defined records and created classes as
well.

Ah well, really must read a good book on Delphi.

Mark Harby
Nottingham. UK


Andy Hill

غير مقروءة،
16‏/11‏/2001، 4:58:46 ص16‏/11‏/2001
إلى
Did you typecast on the way back. As in:


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;

godsg...@gmail.com

غير مقروءة،
25‏/02‏/2016، 8:19:13 ص25‏/2‏/2016
إلى
I was looking for the same thing and your Example worked - thanx :D
0 رسالة جديدة