Why is FieldAddress nil

Delphi 7.
Create a new Application and place a Button on a Form.
Double-click on the Button to enter the following code on the OnClick event:

// start
if Button1.FieldAddress('Caption') <> nil then
    ShowMessage('You solved this problem!');
// end

Compile, run, and click on the Button. The message does not show.

Why is the returned pointer always nil?!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Delphi help says:
  Programs should access and manipulate fields by using properties instead of FieldAddress.

Do you want to get the value of 'Caption' property?

If yes, add "typinfo" unit in your uses clause and try this:

  ShowMessage(GetPropValue(Button1, 'Caption'));
pt_pteroAuthor Commented:
Thanks for your comment, Anderson.

Using RTTI does not address my problem. RTTI works for TComponent descendents and I need this for objects of type such as TStringList.

Place a TMemo in the form and:

  strList1 := TStringList(Memo1.Lines);
  ShowMessage(strList1.Text);                           // this works
  ShowMessage(GetPropValue(strList1, 'Text'));   // this fails

The question remains: Why is the returned pointer of FieldAddress always nil?! I used TButton to simplify. My question addresses FieldAddress.

Thanks again
Kyle FosterCEOCommented:
have you looked at the following thread.


This should at least give you an idea of what issues the other guy(gal) ran into.  I am currently having the same problem as you when I test the FieldAddress function.  I am hacking away at it, but I Haven't used assembler in years.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

pt_pteroAuthor Commented:

Thanks for your link. I already had looked at that thread and it didn't help. I got to the same point of trying to understand the assembly for I simply don't have the time for it now.

Please let us know if you find a satisfying answer for this (or not).

Thanks again
Kyle FosterCEOCommented:
I will play with it tomorrow.  Brain dead from the week.  It is annoying me, so I will figure it out.
Kyle FosterCEOCommented:
All right.  I am getting the same result as you.
Can you give me more detail on what you are trying to do?  
Also, I am in D7.  What version of Delphi are you running.  This appears to have been broken sometime down the line.

FieldAddress looks up the property in the VMT table and returns the address of the property. (at least in theory, and it used too).

I created a descendent of TBUtton and added a property TestInt and tried to call fieldaddress to see if the property couldnt be inherited.  This gave me the same result.

I used this

    Field : PInteger;
  lPropInfo: PPropInfo;
  lPropInfo := GetPropInfo(Button1, 'Enabled');
  if Assigned(lPropInfo) then
    Memo1.Lines.Add('Enabled is published')
    Memo1.Lines.Add('Enabled is not published');
  lPropInfo := GetPropInfo(Button1, 'enabled');
  if Assigned(lPropInfo) then
    Memo1.Lines.Add('enabled is published')
    Memo1.Lines.Add('enabled is not published');

to find a property and it worked fine.  This was from Borland where a user was complaining that in Delphi 2005 the GetPropInfo is case sensitive.

lPropInfo gives you the address of both the Get... and the Set... procs.

so I tried to add a static field to my TMyButton

    TestField : Integer;

and was told that I cant add static properties.  This makes sense, so what are you expecting to get back from FieldAddress?  a pointer to the property info, or the address of fTestField : Integer;  I would expect the later, but with a property there may not be an address.

I created
  TTestButton = class(TButton)
    { Private declarations }
    { Protected declarations }
    { Public declarations }
    fTestField : Integer;
    { Published declarations }
    property TestField : Integer read fTestField write fTestField;
 and tried
  Field := TestButton1.FieldAddress('TestField');

guess what...  Field=nil

--->  Still trying.  Can you let me know what you have tried so I don't refail.  :)
Kyle Foster
Kyle FosterCEOCommented:
I did some digging in the Delphi Source.

FieldAddress is used to tie a declaration of an object in the dfm to the instance variable.

I stepped through TComponent.SetReferrence and it is getting back a result.  So I Tried from the button to get a reference to the Buttons instance variable and it returned me the TButton.

  Field: ^TComponent;
  if Owner <> nil then
    Field := Self.FieldAddress('Button1');
    if Field<>nil then

But this can't work for a property because there is no real fieldaddress.  The set and get encapsulate this from us.
Kyle FosterCEOCommented:
The FieldAddress function is looking at the property NAME to find the pointer.  TOjects don't have a name property therefore it can't work.  I

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.