Link to home
Start Free TrialLog in
Avatar of dondeb1
dondeb1

asked on

Info about property set function in class modules in VBA

VBA for Access (but as a larger question for VBA and VB in general.)

The documentation I have access to gives a rather vague explanation of exactly what the property set function in a class module does and how it relates to the set statement in the standard module that calls it. I don't expect a full tutorial here, but perhaps a souce where I could find a (few) very simple example (so I don't have to wade through a lot of difficult code) in order to see how it works.

It seems to set a reference to an object. After that  is done, where exactly is the object available? Is it available to the standard module, or available only to the class module? This is one of those questions that you are not even sure what the question shoul be because the info is so scanty.
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Set is used in the basically the same way ... in that in both cases you are referring to an object variable you have defined ... and where that variable is available depends on the scope you defined ... Private, Public, Friend.

In a Class Module (which included Form and Report modules), you have Property Set, Property Let and Property Get.  Set is used is the same way ... to refer to an object variable ( such as a form, recordset, activeX control, etc) as opposed to a variable such as an Integer, String etc.

mx

Even though both contain the word set, they are used a little differently.  Set on it's own is used to link a variable to an object.  ie.

  Dim rst as DAO.recordset

  Set rst = DAO.OpenRecordset(....

  Here the variable being 'rst'.   It's scope depends on where rst was Dim'd.  If in the procedure, then it's local and will disappear when the procedure terminates.  if in a form or report, it's when that object closes.  And if in a module, it's for the life of the app.

  Property Set is a bit different.  What your doing is that your defining a property that will appear in a class and that property is a reference to an object.  That would look like this:

Property Set Pen(P As Object)

    Set CurrentPen = P    ' Assign Pen to object.

End Property

  Now when I create an instance of this class, it will have a property of .Pen, and that property will be working with an object.  The companion to that is Property Let.   Again you are defining the property of a class, but this time with a variable:

Property Let PenColor(ColorName As String)
    Select Case ColorName    ' Check color name string.
        Case "Red"
            CurrentColor = RED    ' Assign value for Red.
        Case "Green"
            CurrentColor = GREEN    ' Assign value for Green.
        Case "Blue"
            CurrentColor = BLUE    ' Assign value for Blue.
        Case Else
            CurrentColor = BLACK    ' Assign default value.
    End Select
End Property

   In this case, CurrentColor is the variable that is storing the value for this property.  Again when you create an instance based on this class, it will have a property of .PenColor.

  Property Get is the finial piece of defining properties for classes.  It looks like this:

' Returns the current color of the pen as a string.
Property Get PenColor() As String
    Select Case CurrentColor
        Case RED
            PenColor = "Red"
        Case GREEN
            PenColor = "Green"
        Case BLUE
            PenColor = "Blue"
    End Select
End Property

  In this case, were fetching the value of the property of a class.  internally within the class, that property is stored in the variable CurrentColor.

  So poperty set is is used as part of defining a class and it's properties.  SET on it's own is used outside a class to get a reference to an object variable.  Quite similar, but used for two very different things.

HTH,
JimD

 



Avatar of dondeb1
dondeb1

ASKER

JDettman,

Your answer is very helpful but I wonder if you might flesh out this part a bit more.

Quote -----------------------------------------------------------------------------
  Property Set is a bit different.  What your doing is that your defining a property that will appear in a class and that property is a reference to an object.  That would look like this:

Property Set Pen(P As Object)

    Set CurrentPen = P    ' Assign Pen to object.

End Property
______________________________________________________________---------

The "P as object" being referenced, is that a part of an outside object being instantized within the class module, or is it part of the class module itself or is it something else entirely. That is not clear from your answer. I just need to know a bit more about what and where the objects, their relation to the class module and the outside code using the instantiation object, and what are the events surrounding this function. Does it get called from outside the module and result in some internal event? I am very new to writing class modules, in fact, this is my first effort.  
 
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dondeb1

ASKER

This question is such that there is no clear meaning for "complete" since it was pretty much open-ended. But the answer did give a sufficient handle on the subject to be very useful. If there were several gradations of "partial" instead of only one it would have been graded much nearer the "yes" end instead of in the middle. Thanks.