Página 1 de 1
Basic syntax for own classes ??
Publicado: Vie Jun 14, 2013 2:54 pm
por ChrisGillard
Hi,
I have gone backwards in my knowledge of how to write simple classes, particularly the New & Create Constructor methods
I am using Harbour & Xailer 3.02.
These classes do not inherit from windows GUI classes, they are just a few vars and methods to represent a business entity.
In my code I think I want to write this ....
oCar := Car():New( "Ford, "T", 4 )
My class looks like this ....
My questions are now that we are using Harbour :
- Should all my classes non windows GUI classes inherit from TComponent so I can use the debugger on them?
- If I inherit from TComponent, I probably need a ::parent or owner?
- Do I need a New method and what should it look like?
- Do I need a Create method and what should it look like?
- When do I need a Super():Create()?
- When do I need a Destructor ... very tricky this one?
Can you suggest a simple class in Source that is a good model to use of how it should be done in 2013.
Apologies for what may be very obvious to most of you.
Regards
Chris
Attached files
Basic syntax for own classes ??
Publicado: Vie Jun 14, 2013 4:27 pm
por ignacio
ChrisGillard escribió el vie, 14 junio 2013 14:54My questions are now that we are using Harbour :
>>Should all my classes non windows GUI classes inherit from
>>TComponent so I can use the debugger on them?
No, it is not necessary. I believe all the class members from non Tcomponent inherited classes are completely shown on the debugger. In case not just use the command PROPERTY instead of DATA.
>>If I inherit from TComponent, I probably need a ::parent or owner?
No. It is not necessary.
>>Do I need a New method and what should it look like?
No. You do not needed. But if you plan to use it and inherit from TComponent, then I suggest you finish the code with this line:
RETURN ::Super:New( oParent )
>>Do I need a Create method and what should it look like?
The same as above.
>>When do I need a Super():Create()?
When you want your class to execute the code from inherited classes. BTW, use '::Super'
>>When do I need a Destructor ... very tricky this one?
Almost never. We do not use it in Xailer, since we can not control WHEN that code is executed. We prefer to destroy on the Free() method.
>>Can you suggest a simple class in Source that is a good model
>>to use of how it should be done in 2013.
I suggest you take a look to Image.prg. If there is something you do not understand (sure there will be) please ask for advice.
>>Apologies for what may be very obvious to most of you.
No need to apologize. Glad to help.
Regards,
Basic syntax for own classes ??
Publicado: Vie Jun 14, 2013 6:27 pm
por ChrisGillard
Hi Ignacio,
Thanks for your detailed reply and I am now being more successful by NOT inheriting from TComponent and using a
METHOD Init CONSTRUCTOR.
My next problem is defining an event.
The Class is a ReportField object like your RptColumn.
I am defining an OnPrintData event like yours
In my class definition I have ( same as you, but simpler for moment) .....
EVENT OnPrintData( oSender, @xString, Value )
In My Print Method, I have in PUBLISHED section .....
::OnPrintData( oSender, @xString, value )
In my test application I have .....
aFields[1]:OnPrintData := { | oSender, xString, Value | xString := "999" }
If I put a logdisplay in the block above I can see that xString & Value are correct at runtime.
But my changes to the passed by reference xString do not appear back in my Print Method .... xString & Value are unchanged.
I have studied your code and I think I am the same.
What am I doing wrong please.
Thanks
Chris
Basic syntax for own classes ??
Publicado: Sab Jun 15, 2013 1:57 pm
por ChrisGillard
!!!!! Solved It !!!!!
if I define my event as ....
EVENT OnPrintData( @xString, Value )
When I use it the oSender comes through as the first parameter automatically!
oRepFld:OnPrintData := { | oSender, xString, Value | xString := "999"
The third parameter is as expected.
So when defining an EVENT you don't need the oSender, it is added for free!
Think I'm done now.
Chris
Basic syntax for own classes ??
Publicado: Sab Jun 15, 2013 5:07 pm
por ignacio
ChrisGillard escribió el sáb, 15 junio 2013 13:57!!!!! Solved It !!!!!
if I define my event as ....
EVENT OnPrintData( @xString, Value )
When I use it the oSender comes through as the first parameter automatically!
oRepFld:OnPrintData := { | oSender, xString, Value | xString := "999"
The third parameter is as expected.
So when defining an EVENT you don't need the oSender, it is added for free!
Think I'm done now.
Chris
That's it. Glad you solve it!
Regards,