xsharp.eu • Extended VO Classes with own Methods
Page 1 of 1

Extended VO Classes with own Methods

Posted: Mon Apr 22, 2024 11:42 am
by lagraf
What can I do if I extended some VO Base Classes (__FormDialogWindow, Control) with own Methods:
  • Subclassing is not a good idea with base classes
  • How does it work with the transporter generated Control_external_class, is this an easier way
Franz

Re: Extended VO Classes with own Methods

Posted: Mon Apr 22, 2024 11:44 am
by wriedmann
Hallo Franz,
wenn Du nicht auf interne Variablen der Klasse zugreifen musst, sind extension methods das Beste.
https://docs.xsharp.it/doku.php?id=extension_methods
Wolfgang

Re: Extended VO Classes with own Methods

Posted: Mon Apr 22, 2024 11:46 am
by wriedmann
Hallo Franz,
noch was: das, was der XPorter macht, ist nur ein Workaround, damit sich der Code kompilieren lässt - damit funktioniert Dein Code sicher nicht.
Wolfgang

Re: Extended VO Classes with own Methods

Posted: Mon Apr 22, 2024 12:51 pm
by lagraf
Hallo Wolfgang,
ich habe die Methode für die Control Klasse dann so implementiert:

Code: Select all

STATIC CLASS ControlExtensions
STATIC METHOD PostFocus() 
    PostMessage( GetParent(SELF:Handle()) , WM_NextDlgCtl , DWORD( _CAST , SELF:Handle() ) , 1L )
    IF IsInstanceOf(SELF,#SingleLineEdit)
        PostMessage(SELF:Handle(), EM_SETSEL, 0, LONG(_CAST,SLen(RTrim(SELF:TextValue))))
        PostMessage(SELF:Handle(), EM_SCROLLCARET, 0, 0L )
    ELSEIF IsInstanceOf(SELF,#MultiLineEdit)
        PostMessage(SELF:Handle(), EM_SETSEL, 0, 0L )
        PostMessage(SELF:Handle(), EM_SCROLLCARET, 0, 0L )
    ENDIF
    RETURN NIL
END CLASS
Damit müßte die Methode PostFocus() bei allen von Control abgeleiteten Klassen verfügbar sein, richtig?

Re: Extended VO Classes with own Methods

Posted: Mon Apr 22, 2024 1:11 pm
by wriedmann
Hallo Franz,
nein, so funktioniert das leider nicht.
"self" gibt es in Deiner statischen Klasse nicht.
Das gehört so:

Code: Select all

STATIC CLASS ControlExtensions
STATIC METHOD PostFocus( self oControl as Control ) as void
    PostMessage( GetParent(oControl:Handle()) , WM_NextDlgCtl , DWORD( _CAST , oControl:Handle() ) , 1L )
    IF IsInstanceOf(oControl,#SingleLineEdit)
        PostMessage(oControl:Handle(), EM_SETSEL, 0, LONG(_CAST,SLen(RTrim(oControl:TextValue))))
        PostMessage(oControl:Handle(), EM_SCROLLCARET, 0, 0L )
    ELSEIF IsInstanceOf(oControl,#MultiLineEdit)
        PostMessage(oControl:Handle(), EM_SETSEL, 0, 0L )
        PostMessage(Control:Handle(), EM_SCROLLCARET, 0, 0L )
    ENDIF
    RETURN
END CLASS
Wolfgang

Re: Extended VO Classes with own Methods

Posted: Tue Apr 23, 2024 3:52 pm
by lagraf
Hi Wolfgang,
thank you for the code, but I think you meant oControl in "PostMessage(Control:Handle(), EM_SCROLLCARET, 0, 0L )".
It compiles without error in this Method.
When I have corrected the remaining errors in this app I will see if it runs.
Franz

Re: Extended VO Classes with own Methods

Posted: Tue Apr 23, 2024 6:16 pm
by wriedmann
Hi Franz,
yes, of course, you are correct.
This was a typing error on my side.
Wolfgang