Jump to content
alnickels

does a class property or a variable exist

Recommended Posts

Yes, it does (but I don't know since when):

 

Type
 TMyClass = Class
 public
  Class Var Counter: Integer;
  Constructor Create; ReIntroduce;
 End;

Constructor TMyClass.Create;
Begin
 Inc(Self.Counter);
End;

Procedure TForm1.Button1Click(Sender: TObject);
Var
 v1, v2: TMyClass;
Begin
 v1 := TMyClass.Create;
 v2 := TMyClass.Create;
 ShowMessage(IntToStr(TMyClass.Counter)); // Will show 2
End;


 

Share this post


Link to post
45 minutes ago, alnickels said:

Is there a way to tell if a variable or class property exist within delphi at runtime?

Are you looking for an RTTI solution to detect at run-time whether or not a given class type has declared a given class variable/property at compile-time?  What is the end goal here that you are trying to accomplish?

Edited by Remy Lebeau

Share this post


Link to post

Remy that will work.    The application will read xml and get a variable or property name.  If variable or property exists we will take certain actions depending on its value.

 

Share this post


Link to post
1 hour ago, alnickels said:

Remy that will work.    The application will read xml and get a variable or property name.  If variable or property exists we will take certain actions depending on its value.

Well, that is pretty easy to accomplish using TRttiContext.GetType() or TRttiContext.FindType(), and then using TRttiType.GetField() and TRttiType.GetProperty().

Edited by Remy Lebeau
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×