Connecting Tech Pros Worldwide Forums | Help | Site Map

How to declare default method/property in Class module?

FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#1: Aug 18 '07
Hi, everybody.

The thread title is actually the whole question. :)
Does anybody know how to declare default method/property in Class module if it is possible at all?
Any ideas/hints/links will be greatly appreciated.

Regards,

Fish

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#2: Aug 18 '07

re: How to declare default method/property in Class module?


Quote:

Originally Posted by FishVal

Hi, everybody.

The thread title is actually the whole question. :)
Does anybody know how to declare default method/property in Class module if it is possible at all?
Any ideas/hints/links will be greatly appreciated.

Regards,

Fish

According to the documentation, if an Object has a Value Property, then this Property is the Default Value for the Object. If an Object does not have a Value Property, than that Object does not have a Default Value. On the down side, I actually tested this, and found I explicitly had to state the Value Property in order to reference it.
Expand|Select|Wrap|Line Numbers
  1. 'Declare a Class Variable
  2. Dim MyClass As clsTest
  3.  
  4. 'Instantiate the Class
  5. Set MyClass = New clsTest
  6.  
  7. 'Assignment to Value Property
  8. MyClass.Value = 123
  9.  
  10. Debug.Print MyClass ==> generates Runtime Error
  11. Debug.Print MyClass.Value ==> returns 123
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#3: Aug 19 '07

re: How to declare default method/property in Class module?


Quote:

Originally Posted by FishVal

Hi, everybody.

The thread title is actually the whole question. :)
Does anybody know how to declare default method/property in Class module if it is possible at all?
Any ideas/hints/links will be greatly appreciated.

Regards,

Fish

Hello FishVal:
I have the correct response to your question. As soon as Mary gives me the OK, I'll be posting it as the Tip of the Week #25. I hope you don't mind sharing the answer with the rest of the world (LOL).
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#4: Aug 19 '07

re: How to declare default method/property in Class module?


Quote:

Originally Posted by ADezii

Hello FishVal:
I have the correct response to your question. As soon as Mary gives me the OK, I'll be posting it as the Tip of the Week #25. I hope you don't mind sharing the answer with the rest of the world (LOL).

Thanks, thanks, thanks
I'm very intrigued. What is it supposed to be? Can't wait to see the post. LOL
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#5: Aug 19 '07

re: How to declare default method/property in Class module?


Quote:

Originally Posted by FishVal

Thanks, thanks, thanks
I'm very intrigued. What is it supposed to be? Can't wait to see the post. LOL

Since you are so intrigued, I decided to post it now for your viewing pleasure. (LOL).

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.
FishVal's Avatar
Expert
 
Join Date: Jun 2007
Location: Israel
Posts: 2,584
#6: Aug 19 '07

re: How to declare default method/property in Class module?


Quote:

Originally Posted by ADezii

Since you are so intrigued, I decided to post it now for your viewing pleasure. (LOL).

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.

Wow! Excellent.

ADezii you are great. Many thanks.

To tell the truth, I was almost sure that VBA doesn't allow this. "Almost", bkz I couldn't believe it. :)
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,429
#7: Aug 19 '07

re: How to declare default method/property in Class module?


Quote:

Originally Posted by FishVal

Wow! Excellent.

ADezii you are great. Many thanks.

To tell the truth, I was almost sure that VBA doesn't allow this. "Almost", bkz I couldn't believe it. :)

Just in case you're interested, Tip #25 has been posted. I hope you didn't mind the fact that I mentioned your name as the motivation for this Tip. Take care.
Reply