Extended VO Classes with own Methods

Deutschsprachiges X#-Forum – German language forum

Moderator: wriedmann

Post Reply
lagraf
Posts: 452
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Extended VO Classes with own Methods

Post 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
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Extended VO Classes with own Methods

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Extended VO Classes with own Methods

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
lagraf
Posts: 452
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Re: Extended VO Classes with own Methods

Post 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?
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Extended VO Classes with own Methods

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
lagraf
Posts: 452
Joined: Thu Jan 18, 2018 9:03 am
Location: A

Re: Extended VO Classes with own Methods

Post 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
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: Extended VO Classes with own Methods

Post by wriedmann »

Hi Franz,
yes, of course, you are correct.
This was a typing error on my side.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply