473,591 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Declare a Default Property in a Class Module

ADezii
8,834 Recognized Expert Expert
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 25532

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

Similar topics

2
7662
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 create objects that reflect data gathered in web inquiries. The module has a bunch of Get/Let routines such as this pair: Private m_dYield As Double
1
1681
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 (me.LoginName) FOR EXAMPLE: Class MyClass Private _loginName As String Public Property LoginName() As String
42
4927
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
3
2478
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 the properties is a follows (irrelevant code omitted):
2
3045
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
4020
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 types of objects (classes) can only be defined in Class modules.
1
1818
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 requirement is like this: I created 4 classess in CCW namely: PdfDocument, PdfForm,PdfFormField and PdfFormFieldList. to the respective .Net classess. Below, am giving the classess in brief. I configured my project for COM Interop . ...
6
9085
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 be greatly appreciated. Regards, Fish
1
1588
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 the upper module CM |_> SCM Form 2 loads first and accesses SCM through CM (everyting OK at this point)
0
7870
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3850
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.