Jump to content
TimC

Newbie Question - VCL Object Rename

Recommended Posts

Is there a setting or a shortcut that will rename all references to a VCL object that has been renamed?

 

Thanks in Advance

-Tim C.

Share this post


Link to post
Posted (edited)

If you simply rename the object manually, then you have to update all references manually.  A search & replace may help, but is spotty and may have to be performed multiple times depending on how many files you need to update..

 

To rename everything automatically, you need to use the Refactor > Rename feature and let it update all of the references for you.

Edited by Remy Lebeau

Share this post


Link to post
2 hours ago, David Heffernan said:

Another good reason not to write code which depends on the string name of objects. 

Hm, and how is it done? I can't imagine.

Share this post


Link to post

Mr. Heffer

 

How does one write code not relying on object names?

 

Share this post


Link to post

Thank You Remmy

Share this post


Link to post

I found (3) techniques that allow one to access an object by other than its name BUT there seems to be one point in each of these techniques that require the object name to be changed in a single spot, essentially the object name is linked to an abstraction layer of sortS (COURTESY OF CHAT GPT:

 

 

1. USING OBJECT REFERENCE

 

var
  MyObject: TMyObject;
begin
  MyObject := Form1.SomeObject;
  MyObject.Property := Value;
  MyObject.Method;
end;

 

2. USING AN INTERFACE

 

var
  MyInterface: IMyInterface;
begin
  MyInterface := Form1.SomeObject as IMyInterface;
  MyInterface.Property := Value;
  MyInterface.Method;
end;
 

 

3. USING A LOOKUP TABLE

 

var
  ObjectDictionary: TDictionary<string, TObject>;
begin
  ObjectDictionary := TDictionary<string, TObject>.Create;
  ObjectDictionary.Add('ObjectKey', Form1.SomeObject);
  
  (ObjectDictionary['ObjectKey'] as TMyObject).Property := Value;
  (ObjectDictionary['ObjectKey'] as TMyObject).Method;
  
  ObjectDictionary.Free;
end;
 

 

4.  USING RUNTIME TYPE INFORMATION:

 

uses
  RTTI;

var
  Context: TRttiContext;
  RttiType: TRttiType;
  RttiProp: TRttiProperty;
  Obj: TObject;
begin
  Obj := Form1.SomeObject;
  RttiType := Context.GetType(Obj.ClassType);
  RttiProp := RttiType.GetProperty('Property');
  if Assigned(RttiProp) then
    RttiProp.SetValue(Obj, Value);
end;
 

At first glance, I am drawn to the idea of the LOOKUP Table, it seems like a pretty clean way to setup a central point to manage object reference

 

Any comments on this topic from the group?

Best Regards

-Tim C.

 

Share this post


Link to post
1 hour ago, TimC said:

Mr. Heffer

 

How does one write code not relying on object names?

 

You use variables which contain references to them. 

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

×