473,236 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,236 developers and data experts.

How to Declare a Default Property in a Class Module

ADezii
8,834 Expert 8TB
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
0 25337

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

Similar topics

2
by: Enigman O'Maly | last post by:
I'm still somewhat new to object style programming (as will become evident), using VBA in Excel 2000 to automate some previously manual functions. I've defined a class module so that I can...
1
by: SteveS | last post by:
What's the difference in using the private variable as opposed to the actual property in a class module? Which is better or more efficient? ( LoginObject.Save (_loginName) or LoginObject.Save...
42
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
3
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of...
2
by: Jon Paal | last post by:
Compiler Error Message: BC30367: Class 'System.Web.HttpContext' cannot be indexed because it has no default property. how do I create a "default property" ??
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
1
by: murtyin | last post by:
Hi to All, I am creating COM Wrappers(CCW's) to the existing .Net Product, which is developed using C#. In this, am getting problem with setting "Item" propertty as the Default Property. My...
6
FishVal
by: FishVal | last post by:
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...
1
by: c2015 | last post by:
I need help with some VB code. I will keep it generic and clear as possible. I have two forms lets call Form1 and Form2 I have a class module (CM) and a sub class module (SCM) instantiated in...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.