Re: Using public members of a class inside the class




Hi Jezebel,

Thank you for your explanation.

This thread is gone off-topic, but still I'd like to make a final remark. In my program, my class sets and reads the text in several cells of a table placed in a document it describes. Thus, I have coded the property GetTextFromRange() as read/write. When you say that it rather looks like a function I wonder if it's because of the (misleading) identifier I used, which implies an process. In actuality, I think it would be more appropriate to write the whole thing as follows:

'Class module
Public Property Let TargetText (StringA as String)

StringA = Trim(StringA)
Activedocument.Tables(1).Columns(2).Cells(3).Range.Text = StringA

End Property

Public Property Get TargetText () as String

TargetText = Activedocument.Tables(1).Columns(2).Cells(3).Range.Text

End Property
'End of class module

This way it is clearer that the code mainly manipulates a property. Would you still implement it as a function instead?

Regards,

Guillermo

Jezebel wrote:
You're right about functions and properties being to some extent a matter of preference; but there is an important conceptual difference in object-oriented programming (which is what you are doing with VBA).

Properties are values, derived by calculation perhaps, but essentially static. They can be set as well as read (unless you choose to make the read- or write-only). When you query a property, you are asking for some information about the thing that has that property. In your example, GetTextFromRange isn't a property of anything (and it would make no difference where you put the function); nor would it make any sense to *set* the GetTextFromRange property.

Functions are methods. They are an instruction to *do* something. If the something is to perform a calculation, then the effect is -- as you observe -- essentially the same as a property.

Consider the objects of Word itself. If you look at the documentation, each object has properties and methods. The methods are coded as functions.




"Guillermo López-Anglada" <guillermo@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:OTTqtI1DHHA.3596@xxxxxxxxxxxxxxxxxxxxxxx
I mainly used the "me" qualifier for clarity, but actually it would seem that it saves you some typing as well in the IDE...

I have no formal training in programming, but isn't the difference between functions and properties a matter of preference to a certain extent? I mean, in my particular case, my property ·always· retrieves its value from the same range... Anyway, I'd very much like to hear any brief explanation on the topic.

Thanks,

Guillermo

Jezebel wrote:
No reason why not. It's even fairly common: you have some standard function that you use throughout the app; it's just as useful from within the module as elsewhere; and for consistency's sake it's better, as far as possible, always to use the same method.
You don't need the 'me', BTW. And your example looks more like a function than a property.




"Guillermo López-Anglada" <guillermo@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:uaGfHM0DHHA.4144@xxxxxxxxxxxxxxxxxxxxxxx
Hello,
I have a class like this:
'Class module
Public Property Get GetTextFromRange() as String
'Code
End Property
Public Function ProcessText() as String
'For example:
ProcessText = Left$(Me.GetTextFromRange,5)

End Sub
'End of Class module
Is it a good practice to use public members of a class like that?
Regards,
Guillermo



.



Relevant Pages

  • Re: Open File Dialog Filter
    ... Double checked class module name, ... >>> Dim cFileOpen As clsGetOpenFileName ... As String ... Public Property Let FileType ...
    (microsoft.public.excel.programming)
  • Re: Opinion from Doug Robbins and Peter Hewett Requested
    ... If you're not sure what going on create a new Template/Project and add the Class Module ... >Public Sub DoSomething() ... >>Private mstrFile As String ... >>Public Property Let Path ...
    (microsoft.public.word.vba.general)
  • Re: Reproducable Hard Crash in Excel 2003 (and earlier versions) with the following VBA code.
    ... Private m_patternLabel As String ... Public Property Get PatternLabel() As String ... Public Property Let PatternLabel ... > correctly It's just a special type of class module. ...
    (microsoft.public.excel.programming)
  • Re: VB 6 and CheckSpelling
    ... Here's a class module that I use: ... Private m_sTextCorrected As String ... Public Function PerformSpellCheckAs PerformSpellCheckConstants ... Public Property Get TextCorrectedAs String ...
    (microsoft.public.word.vba.general)
  • Re: VB 6 and CheckSpelling
    ... Here's a class module that I use: ... Private m_sTextCorrected As String ... Public Function PerformSpellCheckAs PerformSpellCheckConstants ... Public Property Get TextCorrectedAs String ...
    (microsoft.public.vb.general.discussion)