Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Info about property set function in class modules in VBA

Posted on 2007-12-02
5
Medium Priority
?
3,812 Views
Last Modified: 2013-11-27
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.
0
Comment
Question by:dondeb1
  • 2
  • 2
5 Comments
 
LVL 75
ID: 20391771
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

0
 
LVL 58
ID: 20391854
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

 



0
 

Author Comment

by:dondeb1
ID: 20393028
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.  
 
0
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 20398373
<<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.>>

  It's an outside object pointer that's being passed in.  Let's say you had a class that worked with processing text files and you wanted users of your class to be able to store a reference to a file.  You might setup something like this:

Private objSaveFile as TextFile

Property Set SaveFile (objFile as TextFile)

  Set objSaveFile =  objFile

End Property

  Now in code, someone using your class could do this:

  Set objDoc.SaveFile = New TextFile

  We now have a reference to a specific instance of the class TextFile and I can reference any of it's methods and properties through the object reference of SaveFile in my class.  ie.

  objDoc.SaveFile.Path = "C:\myApp\myApp.txt"
  objDoc.SaveFile.FileOpen

   .Path being a property of the object TextFile and .FileOpen one of it's methods.  Neither of those is part of our class; we just provided a means of pointing to an object with the property .SaveFile.

HTH,
JimD  
0
 

Author Closing Comment

by:dondeb1
ID: 31412202
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question