There was a thread on this a few weeks ago and I
thought I'd give it a try in my spare (?) time.
Defined
type
TFooObject = class(TObject)
id : integer;
desc :
string;
role :string;
end;
Then a procedure
var
MyObj : TFooObject; <some code>
MyObj.id := idfromsomewhere;
MyObj.desc := descfromsomewhere; MyObj.role := rolefromsomewhere; ListBox1.Items.AddObject(text, MyObj); end;
When the user clicks in the list box, I want to use
the object details.
procedure TForm1.ListBox1Click(Sender:
TObject);
var MyObj: TFooObject; XO : TObject; begin MyObj := TFooObject.Create; XO := TObject.Create; XO := ListBox1.items.objects[listBox1.ItemIndex]; lblRole.Caption := TFooObject(XO).typ.Role; end;
Problems:
1. In the line XO :=
ListBox1.items.objects[listBox1.ItemIndex]; - the original example had MyObj :=
... but when I compile the compiler won't allow that as it doesn't see
TFooObject as a TObject.
2. The above code compiles but I get an access
violation at lblRole.Caption := TFooObject(XO).typ.Role;
(I've tried ... := (XO as TFooObject).Role
but no more success)
Any pointers :) as to where I'm going wrong
here.
TIA
Mike
|
- Re: [DUG]: AddObject Mike Osborne
- Re: [DUG]: AddObject Aaron Scott-Boddendijk
- Re: [DUG]: AddObject Mike Osborne