class, classvar, data, etc.

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

Post Reply
User avatar
l3whmg
Posts: 694
Joined: Mon Feb 23, 2009 8:46 pm
Location: Italy
Contact:

class, classvar, data, etc.

Post by l3whmg »

Hi guys, I'm learning about class and I need your help.

A) Into a CLASS I can store data; but what is the correct command?
I look at "CLASSVAR" (if I understand "CLASS VAR" it's the same) and "DATA", but I don't understand very well differences.

B) If I have a CLASS and a sub "CLASS" I can define "SHARED" data in main class; in this way, subclass can use var declared (I look at HMG4). But with SHARED command I can access, to these data, from .prg who use the CLASS (and SUBCLASS). Is there a way to "hidden" this data to .prg but still used in subclass? This question it's a monster of my mind?

Thank a lot in advance.
Luigi from Italy
www.L3W.it
User avatar
dhaine_adp
Posts: 457
Joined: Wed Aug 06, 2008 12:22 pm
Location: Manila, Philippines

Re: class, classvar, data, etc.

Post by dhaine_adp »

Hi Luigi,

CLASSes are great but it is not always a solution to programming problems. You are in advantage if you use them in combination with procedural method. In that way you can get the best of both worlds.

In straight forward manner when you create a CLASS that means its an object. An object has its own data. The data is then can be hidden or public along with the methods that you define. Methods in concept is equivalent with function declaration in a particular PRG source code, but of course they are different in principles. The scope of data and methods in a class are much like public and private variables. So if you look at the source code of HMG4, in Harbour, in Fivewin and in the books they all tell about inherittance, polymorphism and code-reused.

Now to address your question CLASS VAR and DATA are synonym statement (I don't know exactly how it was implemented in Harbour). But anyway you use them to declare the variables of the class. So you see both of them refers to variables which is also the data. To make them hidden or visible is just a matter of how you declare them in your class definition.

Example Only to demostrate the principles:

CLASS Box
HIDDEN:
DATA nLastPosition
DATA nLastHeight
DATA nLastWidth

METHOD AutoResize()

EXPORTED:
DATA nRow
DATA nCol
DATA nHeight
DATA nWidth
DATA nLineSize
DATA aLineColor
DATA aFillColor
DATA lTransparent

METHOD New()
METHOD Show()
METHOD Hide()
END CLASS

So when you create a sublass from that base class it will not inherrit those declared in HIDDEN because it is local to the class. Base classes should not contain a hidden methods and data if you are going to call them in the public (exported methods). Please OOP gurus correct me if I am wrong. I learned OOP in harbour only.

Sorry friend if I am wrong...

Regards,

Danny
Regards,

Danny
Manila, Philippines
mrduck
Posts: 497
Joined: Fri Sep 10, 2010 5:22 pm

Re: class, classvar, data, etc.

Post by mrduck »

dhaine_adp wrote: Now to address your question CLASS VAR and DATA are synonym statement
No, they are not synonims... a DATA member is indipendent in each object (in your example nLastPosition can be assigned different values in two instances of Box) while CLASS VAR variables are unique to all objects of that class, you assign for one object and retrieve the same value in another... they are usually used as counters.

As a sidenote I want to say that some member definition should be modified to PROTECTED or HIDDEN because now the are easily accessible from user code...
User avatar
l3whmg
Posts: 694
Joined: Mon Feb 23, 2009 8:46 pm
Location: Italy
Contact:

Re: class, classvar, data, etc.

Post by l3whmg »

Hi guys,
many thanks to all. Now, I'll try.

Best regards.
Luigi from Italy
www.L3W.it
User avatar
dhaine_adp
Posts: 457
Joined: Wed Aug 06, 2008 12:22 pm
Location: Manila, Philippines

Re: class, classvar, data, etc.

Post by dhaine_adp »

mrduck wrote:
dhaine_adp wrote: Now to address your question CLASS VAR and DATA are synonym statement
No, they are not synonims... a DATA member is indipendent in each object (in your example nLastPosition can be assigned different values in two instances of Box) while CLASS VAR variables are unique to all objects of that class, you assign for one object and retrieve the same value in another... they are usually used as counters.

As a sidenote I want to say that some member definition should be modified to PROTECTED or HIDDEN because now the are easily accessible from user code...
Hi mrduck,

I appreciate your comments. Thank you for correcting me.

Regards,

Danny
Regards,

Danny
Manila, Philippines
Post Reply