Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2010-03

TComponent.ChangeName() not virtual?

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

Below is the definition of TComponent.ChangeName.

TComponent = ...
  ..
protected
  procedure ChangeName(const NewName: TComponentName);
  ...
end;

For improved support in visual form designers [more specifically fpGUI's Form Designer - but this applies to other form designers too], I want to apply alternative changes to the component when the Name of the component changes.

The logical thing for me would have been to override ChangeName, checking for csDesigning and apply my changes as needed.

Unfortunately ChangeName is not virtual, yet it is in the 'protected' section of a class. If we are not supposed to override ChangeName, then why is it in 'protected' section and not 'private' section of the class definition?

Is this simply an oversight? Can the ChangeName method become virtual?

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Vincent Snijders
to Graeme Geldenhuys
Graeme Geldenhuys wrote
Unfortunately ChangeName is not virtual, yet it is in the 'protected' section of a class. If we are not supposed to override ChangeName, then why is it in 'protected' section and not 'private' section of the class definition?

Obviously, so that descendants can call it.

Vincent

Reply by email

Graeme Geldenhuys
to Vincent Snijders

Vincent Snijders het geskryf:

Obviously, so that descendants can call it.

I see TComponent also has a SetName() method [the actual setter method for the Name property], and the SetName() method actually calls the ChangeName() method.

  procedure SetName(const NewName: TComponentName); virtual;

The TComponent.SetName is virtual, so that solves my problem, but I still don't see why ChangeName is even there? Why have two methods to do one task? What purpose does ChangeName() have? Why couldn't SetName() set the internal field variable FName?

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Paul Ishenin
to Graeme Geldenhuys
Graeme Geldenhuys wrote 5 lines
The TComponent.SetName is virtual, so that solves my problem, but I still don't see why ChangeName is even there? Why have two methods to do one task? What purpose does ChangeName() have? Why couldn't SetName() set the internal field variable FName?

SetName obviosly more than just set FName. And how to protect FName and at the same time give a way to change it without hooking the events?

Best regards,
Paul Ishenin.

Reply by email

Graeme Geldenhuys
to Paul Ishenin

Paul Ishenin het geskryf:

> SetName obviosly more than just set FName. And how to protect FName and at the same time give a way to change it without hooking the events?

OK, that make sense, but then the same could be accomplished by simply moving FName to the protected section. After all, nobody can override ChangeName() because its not virtual, so all that it will ever do is just set the FName field variable. A developer wanting to bypass the code in SetName, could then simply set FName directly - and get rid of the pointless ChangeName method..

Either way, my initial problem has been solved by overriding the SetName() method.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email