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
6 8860
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. - 'Declare a Class Variable
-
Dim MyClass As clsTest
-
-
'Instantiate the Class
-
Set MyClass = New clsTest
-
-
'Assignment to Value Property
-
MyClass.Value = 123
-
-
Debug.Print MyClass ==> generates Runtime Error
-
Debug.Print MyClass.Value ==> returns 123
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).
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
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: - Create the following simple Class named MyClass. MyClass will consist of just 2 Properties: Value (Default) and MyName. It does not contain any Methods.
-
Private pValue As Long
-
Private pMyName As String
-
-
Property Get Value() As Variant
-
Value = pValue
-
End Property
-
-
Property Let Value(ByVal vNewValue As Variant)
-
pValue = vNewValue
-
End Property
-
-
Property Get MyName() As Variant
-
MyName = pMyName
-
End Property
-
-
Property Let MyName(ByVal vNewName As Variant)
-
pMyName = vNewName
-
End Property
- Save your Class Module (MyClass) after creating it.
- From the File menu, choose Remove MyClass.
- When prompted to Export the File First, choose Yes and save the Module.
- Open the exported file (*.cls) in Notepad, or any Text Editor.
- 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.
- Attribute Value.VB_UserMemId = 0
- The Property Get Procedure should now look like the following:
- Property Get Value() As Variant
-
Attribute Value.VB_UserMemId = 0
-
Value = pValue
-
End Property
- Save the file in Notepad, then exit.
- 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.
- The following code block will now work where it previously would not have because Value was not the Default Property of the MyClass Object.
- 'Declare a Variable to refer to the New Instance of
-
'MyClass (a MyClass Object)
-
Dim clsMyClass As MyClass
-
-
'Instantiate the Class
-
Set clsMyClass = New MyClass
-
-
'Can do this because Value is now the Default Property
-
clsMyClass = 9999
-
-
'Must use standard syntax since MyName is not a Default Property
-
clsMyClass.MyName = "Fred Flintstone"
-
-
MsgBox clsMyClass 'returns 9999
-
MsgBox clsMyClass.MyName 'returns Fred Flintstone
- The above code has been tested and is fully operational. To the best of my knowledge, it requires Access 2000 and above to work.
- Should you have any questions feel free to ask.
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: - Create the following simple Class named MyClass. MyClass will consist of just 2 Properties: Value (Default) and MyName. It does not contain any Methods.
-
Private pValue As Long
-
Private pMyName As String
-
-
Property Get Value() As Variant
-
Value = pValue
-
End Property
-
-
Property Let Value(ByVal vNewValue As Variant)
-
pValue = vNewValue
-
End Property
-
-
Property Get MyName() As Variant
-
MyName = pMyName
-
End Property
-
-
Property Let MyName(ByVal vNewName As Variant)
-
pMyName = vNewName
-
End Property
- Save your Class Module (MyClass) after creating it.
- From the File menu, choose Remove MyClass.
- When prompted to Export the File First, choose Yes and save the Module.
- Open the exported file (*.cls) in Notepad, or any Text Editor.
- 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.
- Attribute Value.VB_UserMemId = 0
- The Property Get Procedure should now look like the following:
- Property Get Value() As Variant
-
Attribute Value.VB_UserMemId = 0
-
Value = pValue
-
End Property
- Save the file in Notepad, then exit.
- 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.
- The following code block will now work where it previously would not have because Value was not the Default Property of the MyClass Object.
- 'Declare a Variable to refer to the New Instance of
-
'MyClass (a MyClass Object)
-
Dim clsMyClass As MyClass
-
-
'Instantiate the Class
-
Set clsMyClass = New MyClass
-
-
'Can do this because Value is now the Default Property
-
clsMyClass = 9999
-
-
'Must use standard syntax since MyName is not a Default Property
-
clsMyClass.MyName = "Fred Flintstone"
-
-
MsgBox clsMyClass 'returns 9999
-
MsgBox clsMyClass.MyName 'returns Fred Flintstone
- The above code has been tested and is fully operational. To the best of my knowledge, it requires Access 2000 and above to work.
- 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. :)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |