473,581 Members | 2,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to declare default method/property in Class module?

FishVal
2,653 Recognized Expert Specialist
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
Aug 18 '07 #1
6 9082
ADezii
8,834 Recognized Expert Expert
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
Aug 18 '07 #2
ADezii
8,834 Recognized Expert Expert
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).
Aug 18 '07 #3
FishVal
2,653 Recognized Expert Specialist
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
Aug 19 '07 #4
ADezii
8,834 Recognized Expert Expert
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.
Aug 19 '07 #5
FishVal
2,653 Recognized Expert Specialist
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. :)
Aug 19 '07 #6
ADezii
8,834 Recognized Expert Expert
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.
Aug 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5458
by: Enigman O'Maly | last post by:
I'm trying to convert some VBA routines in an Excel 2000 workbook to use objects instead of global varaibles. I've defined a class module with the following (excerpts): (from SheetMetrics class module) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Private m_HoldingSheetName As String Private m_rngFundSymbols As Range * *
2
2318
by: Sergey Ilinsky | last post by:
Well, I've been working with JS for three years and have a great experience here. But! I still have no really acceptable answer to the following question: What is the principle difference between declaring methods/properties in the constructor function body and via prototypes. Are there any real GURUs? Let's discuss the issue. P.S....
10
3544
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base class *Shared* methods which make use of the class ID. So I gave the base class a classID field, and then I gave the derived classes Shared...
5
1931
by: Harold Hsu | last post by:
Hi all, What's the default access type of a property declared in an interface? The one I'm looking at is IBindingList: Public Interface IBindingList .... ReadOnly Property AllowEdit As Boolean .... End Public
44
2743
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
1
6312
by: Stu | last post by:
Hi, Im using vis studio 2003 and I think wse is out of the question as clients could be using java which doesnt support it. So I managed to find some code which allows you to develop a custom soap header called by using a http module. The problem Im having is I cannot seem to get the event to raise to fire off my authenticate method in the...
15
2984
by: esha | last post by:
I need to have a Public variable in my project. In VB it can be declared in a standard module. Where can I do it in C# ? I tried to do it in default class Program.cs and I tried it in an added by me class. No success So, how and where I declare a variable visible by any module in the project? Esha
10
2500
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under certain circumstances the query string may not contain the variable. So I need some way of establishing a default value if one isn't set. Is...
0
25528
ADezii
by: ADezii | last post by:
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...
0
7854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8134
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8296
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8157
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3807
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1394
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.