ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5538|回复: 8

[转帖] 如何给类模块指定默认的Member

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-8-4 21:14 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖已被收录到知识树中,索引项:类和类模块
本帖最后由 liucqa 于 2013-9-25 11:21 编辑

http://www.cpearson.com/excel/DefaultMember.aspx
http://www.blog.methodsinexcel.co.uk/2008/12/08/setting-the-default-value-of-a-class-in-vba/

Default Member Of A Class

This page describes how to specify a default method of a class.
ShortFadeBar

Introduction

If you are working with classes in VBA (see Class Modules for more details) it is often useful to make one member of a class the default member. For example, in the Excel Range object, the default member is Value. This allows you to omit the member name and use code like the following:

    Range("A1") = 1234
' is the same as
    Range("A1").Value = 1234

Because Value is the default member, it may be omitted in the code. Creating a default member of a class is also very useful (necessary, really) when you are working with a customized Collection class. (See Creating A Custom Collection Class for more information about custom Collection classes.) In this case, you would likely specify the Item method as the default member. This allows you to use code like the following:

    V = Coll(2)
    ' is the same as
    V = Coll.Item(2)

SectionBreak

Creating A Default Member In VBA

VBA does not directly support the creation of a default member of a class. That is, there is nothing in the VBA IDE that allows you to specify a default member. However, VBA does respect the default method if it is specified in a class. To specify a method as the default member, you need to Export the class module to a text file, edit that text file in NotePad or your favorite text editor, add an Attribute directive to the method, and then Import the text file back into the VBA Project.

First, export the class module to a text file. In VBA, go to the File menu and choose Export File.... In the Save dialog that appears, navigate to some folder (it doesn't matter which folder), and save the class file as text with a cls extension. Next, select Remove... from the File menu and choose No in the Do you want to export? dialog. Next, open Notepad ( C:\Windows\Notepad.exe) or another text editor, and open the cls that you saved in the Export step. In the text file, go to the method that you want to make the default, and add the following line of code.

Attribute Value.VB_UserMemId = 0

An Attribute directive is an instruction to the compiler indicating various conditions for compilation. The Attribute directives are not visible in the VBA Editor and they cannot be added by the VBA Editor. You must use a text editor to add Attribute directives. If you are making the Value property the default member of your class, your code in Notepad should look similar to the following:

    Property Get Value() As Long
        Attribute Value.VB_UserMemId = 0
        Value = Whatever
    End Property

You can make a Sub, Function, or Property the default member of the class, but only one procedure in the module may be the default member. Once you have added the Attribute directive to the text file, save the file and exit from NotePad. Now, in the VBA Editor, go to the File menu and choose Import File.... In the Open dialog that appears, navigate to the folder in which you saved the cls file and import it into VBA. Because Attribute directives are not visible in the VBA Editor, you will not see any changes in your code.

Once the Attribute directive is in place, you can use code like the following:

    Dim CC As CMyClassName
    Set CC = New CMyClassName
    CC.Value = 123
    ' is the same as
    CC = 123

This page last updated: 2-May-2008

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-8-4 22:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
失而复得呀。

这个帖子的用途是给类模块指定一个默认的属性或者事件。

在XMLHTTP的OnReadyStateChange事件中可以使用,这样就能实现异步监测XMLHTTP的进度了。对大数据量下载非常有用。

TA的精华主题

TA的得分主题

发表于 2012-8-6 22:19 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2012-8-7 08:21 | 显示全部楼层
为类指定默认属性,这个是在设计阶段修改自己的类。
VBA中要实现的话要先把类模块导出,然后用文本方式打开导出的文件,改好后保存重新导入到VBA中。
在VB中很容易实现,有个"类生成器实用工具"可以直接指定。

TA的精华主题

TA的得分主题

发表于 2012-8-7 09:08 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2014-9-12 16:45 | 显示全部楼层
学习了,已经应用成功。谢谢分享,也多些南宫的解释, liucqa 转的英文看的好累。

TA的精华主题

TA的得分主题

发表于 2014-9-12 17:41 | 显示全部楼层
本帖最后由 yiyiyicz 于 2014-9-13 09:41 编辑
Moneky 发表于 2014-9-12 16:45
学习了,已经应用成功。谢谢分享,也多些南宫的解释, liucqa 转的英文看的好累。


4楼的说明至关重要,否则代码通不过
最后一句实际也是解决方法之一,即将VBA代码导入到VB,修改后再倒回来
在高级VBA开发的教材中有详细的操作步骤,中文翻译本

TA的精华主题

TA的得分主题

发表于 2020-3-20 16:56 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
liucqa 发表于 2012-8-4 22:54
失而复得呀。

这个帖子的用途是给类模块指定一个默认的属性或者事件。

你的这句话看不懂,为什么默认属性或事件和异步有关系??

点评

和类模块有关  发表于 2020-3-22 23:22
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-12-22 23:27 , Processed in 0.034162 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表