RAD HELP: -- TObject is never directly instantiated. Although it does not use programming language features that prevent instantiation, TObject is an abstract class -----
It should be noted that "local variables" are given a random value when not initialized. And, the compiler might be using a location in memory to reference this variable.
However, when you add one more "member" to the "record", this memory location may not be available for use, so the compiler responds as "I CANNOT USE IT IN THIS POSITION".
That is, you cannot "type cast" this value to another type in this memory location.
Remembering that the "TObject" is something like a "does everything (or almost everything)". So it accepts many uses, including this weird conversion you're intending to do: "RECORD" to "TOBJECT"!!!
type TMonster = record ID: integer; end;
procedure TForm1.Button1Click(Sender: TObject); var M : TMonster; Obj: TObject; begin Obj := nil; // // M.ID := 1; // random now... Obj := TObject(M); // ShowMessage(TMonster(Obj).ID.ToString); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
the problem is not a "type" of member, but the "pointer" where it is in. the pointer can appoint to "1234", "12345", "123456"...
then, the value is get these "place" on memory!
the "TObject" dont know what is this!!! because of your natural usage: generic and abstract source for all!!!!
TObject dont have member like: "ID", or any other member like above! then, in runtime will be appointed to any "place on memory" -> pointer -> randomic value... then, if the value is a "integer" type, normally, but dont sure this, the value will be accepted! because a pointer receive a integer value! but not "a string value, or others..."
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
ID:integer; // 4bytes on memory (pointer) ID:string; // 8Bytes on memory (pointer)
this position (used by cast) is like a "variant" var, then, basically, any value is accepted!
after this position, can: -- not exists! -- is another type -- is used for another references
then, the 2nd member (Field) cannot be referenced or used, you see?
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
procedure TForm1.BtnRecordToObjectClick(Sender: TObject); var M : TMonster; Obj: TObject; S : string; begin Obj := nil; // M.ID := 123456; // 123456 = word, integer, cardinal, etc.... // Obj := TObject(M); // // VarAsType(...) result in a "Variant"!!! // // VarAsType(M, varRecord); // "M" not variant // VarAsType(Obj, varRecord); // "Obj" not variant // // S := VarAsType(M.ID, varByte); // overflow --> 123456 > byte // S := VarAsType(M.ID, varSmallint); // overflow --> > smallint // S := VarAsType(M.ID, varObject); // AV --> "integer" into "a type" not possible here!!! // S := VarAsType(M.ID, varString); // OK until 2GB chars // ShowMessage(TMonster(Obj).ID.ToString + ', S=' + S); end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
1) create your RECORD type (with your fields necessary, any type) type R = record ....
2) do the "pointer reference" for this new type Preference = ^R ...
3) create a ARRAY of this "pointer" reference var MyArr[0..n ] of Preference ... // MyArr[ n ] := Preference^.FIELD_NAME_in_RECORD; or MyArr := MyArr + [ Preference^.FIELD_NAME_in_RECORD, .... ];
4) cast this "pointer reference" as TObject, and add this new object in your "ITEMS.object" (listbox, stringlist, etc...) for R in MyArr do begin Obj := TObject( R ).... ListBox1.Items.AddObject( Preference.FIELD_NAME_in_RECORD, Obj); end;
5) to retrieve the values onClick ListBox P := Preference(ListBox1.Items.Objects[ListBox1.ItemIndex])^; showmessage( p.FIELD_NAME_in_RECORD );
then, you can store your "records" as TObject in any "stringlist" (items)
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3