By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,952 Members | 1,593 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to Declare a Default Property in a Class Module

ADezii
Expert 5K+
P: 8,692
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was inadequate to say the least, but I was determined to come up with the correct answer, especially since my primary background is Visual Basic, and I knew that there was an answer. Well, I did come up with an answer and I decided to incorporate it into this week's Tip. I have created a Link to the initial question and response for your reference, after which I'll elaborate on the solution:
Declare a Default Method/Property in a Class Module

VBA does not give you a simple mechanism by which you can specify a Property to be the Default. VBA does, however, support Default Properties but you'll simply have to jump through several hoops to get there. The following steps will describe exactly how to create a Default Property in your Classes:

  1. Create the following simple Class named MyClass. MyClass will consist of just 2 Properties: Value (Default) and MyName. It does not contain any Methods.

    Expand|Select|Wrap|Line Numbers
    1. Private pValue As Long
    2. Private pMyName As String
    3.  
    4. Property Get Value() As Variant 
    5.   Value = pValue
    6. End Property
    7.  
    8. Property Let Value(ByVal vNewValue As Variant) 
    9.   pValue = vNewValue
    10. End Property
    11.  
    12. Property Get MyName() As Variant 
    13.   MyName = pMyName
    14. End Property
    15.  
    16. Property Let MyName(ByVal vNewName As Variant) 
    17.   pMyName = vNewName
    18. End Property
  2. Save your Class Module (MyClass) after creating it.
  3. From the File menu, choose Remove MyClass.
  4. When prompted to Export the File First, choose Yes and save the Module.
  5. Open the exported file (*.cls) in Notepad, or any Text Editor.
  6. In Notepad, find your Property Get Procedure, in this case Value. Add the following line of code on a blank line immediately following the Property Get Statement.

    Expand|Select|Wrap|Line Numbers
    1. Attribute Value.VB_UserMemId = 0
  7. The Property Get Procedure should now look like the following:

    Expand|Select|Wrap|Line Numbers
    1. Property Get Value() As Variant
    2.   Attribute Value.VB_UserMemId = 0 
    3.   Value = pValue
    4.  End Property
  8. Save the file in Notepad, then exit.
  9. In VBA choose Import File and select the file you just modified (MyClass.cls).. You will not see the 'Attribute' Statement in the VBA Editor. The Editor reads and processes Attribute Statements, but does not display them, nor does it allow them to be entered in the Editor.
  10. The following code block will now work where it previously would not have because Value was not the Default Property of the MyClass Object.

    Expand|Select|Wrap|Line Numbers
    1. 'Declare a Variable to refer to the New Instance of
    2. 'MyClass (a MyClass Object) 
    3. Dim clsMyClass As MyClass
    4.  
    5. 'Instantiate the Class 
    6. Set clsMyClass = New MyClass
    7.  
    8. 'Can do this because Value is now the Default Property 
    9. clsMyClass = 9999
    10.  
    11. 'Must use standard syntax since MyName is not a Default Property
    12. clsMyClass.MyName = "Fred Flintstone"
    13.  
    14. MsgBox clsMyClass           'returns 9999
    15. MsgBox clsMyClass.MyName    'returns Fred Flintstone
  11. The above code has been tested and is fully operational. To the best of my knowledge, it requires Access 2000 and above to work.
  12. Should you have any questions feel free to ask.
Aug 19 '07 #1
Share this Article
Share on Google+