Board index » delphi » Components in same unit - how to share common function

Components in same unit - how to share common function

I'd like to place two new components in the same unit and share a common
function, but am having real problems with this.
More specifically, I have TMyNumericEdit derived from TEdit and
TMyDBNumericEdit derived from TDBEdit both in a unit called MyEdits.pas
I also have a function ValidKeyPress which I'd like to use from the
KeyDown and KeyPress events of each component.
The problem is: how does my function know whether it is being called from
the Edit or the DBEdit ?  The function needs to access properties such as
Text, SelStart plus properties of my own such as FAllowNegative etc

I've tried passing it as a parameter of type TCustomEdit (common ancestor
of both) eg ValidKeyPress( TMyDBEdit ) but this generates access
violations, when I try to access a property

Postscript: I have got a solution of sorts: If I use a stub function for
each component to pass properties of the object as parameters to the
function it seems to work OK, but this seems a clunky way of doing this.
Any ideas?

 

Re:Components in same unit - how to share common function


Quote
"David Carle" <d...@davidcarle.com> wrote in message

news:3B427440.6D26@davidcarle.com...

Quote
> I'd like to place two new components in the same unit and share a common
> function, but am having real problems with this.
> More specifically, I have TMyNumericEdit derived from TEdit and
> TMyDBNumericEdit derived from TDBEdit both in a unit called MyEdits.pas
> I also have a function ValidKeyPress which I'd like to use from the
> KeyDown and KeyPress events of each component.
> The problem is: how does my function know whether it is being called from
> the Edit or the DBEdit ?  The function needs to access properties such as
> Text, SelStart plus properties of my own such as FAllowNegative etc

Test the Sender parameter.

Re:Components in same unit - how to share common function


Hello, "David Carle" <d...@davidcarle.com>! You wrote:

Quote
> I'd like to place two new components in the same unit and share a common
> function, but am having real problems with this.
> More specifically, I have TMyNumericEdit derived from TEdit and
> TMyDBNumericEdit derived from TDBEdit both in a unit called MyEdits.pas
> I also have a function ValidKeyPress which I'd like to use from the
> KeyDown and KeyPress events of each component.
> The problem is: how does my function know whether it is being called from
> the Edit or the DBEdit ?  The function needs to access properties such as
> Text, SelStart plus properties of my own such as FAllowNegative etc

> I've tried passing it as a parameter of type TCustomEdit (common ancestor
> of both) eg ValidKeyPress( TMyDBEdit ) but this generates access
> violations, when I try to access a property

> Postscript: I have got a solution of sorts: If I use a stub function for
> each component to pass properties of the object as parameters to the
> function it seems to work OK, but this seems a clunky way of doing this.
> Any ideas?

    You need to pass an object variable to your function and examine  a type
of the variable:
function ValidKeyPress(Sender: TObject; Key: Byte);
begin
 if (Sender is TMyNumericEdit) then begin
  {do something with a MyNumericEdit}
   if TMyNumericEdit(Sender).Text ...........
 end else begin
   {do something with a MyDBNumericEdit}
   if  TMyDBNumericEdit(Sender).FAllowNegative ..........
 end;
end;

    And another one thing - if you want to use private objects' variables
then your function must be placed in the same unit where class is declared.

--
    Evgeny V. Levashov, software developer,
    ICQ UIN 34864288
    Mebel Massiv Co, Tula, Russia
    http://home.tula.net/mebelmassiv

Other Threads