ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

EH搜索     
EH云课堂-专业的职场技能充电站 Excel转在线管理系统,怎么做看这里 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
Excel不给力? 何不试试FoxTable! Excel 2016函数公式学习大典 高效办公必会的Office实战技巧 免费下载Excel行业应用视频
300集Office 2010微视频教程 Tableau-数据可视化工具 精品推荐-800套精选PPT模板,点击获取 ExcelHome出品 - VBA代码宝免费下载
你的Excel 2010实战技巧学习锦囊 欲罢不能, 过目难忘的 Office 新界面 Excel VBA经典代码实践指南
查看: 3561|回复: 6

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

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-8-4 21:14 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:类和类模块
本帖最后由 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 | 显示全部楼层
失而复得呀。

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

在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开发的教材中有详细的操作步骤,中文翻译本
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关注官方微信,每天学会一个新技能

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

GMT+8, 2019-8-23 23:17 , Processed in 0.081274 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2020 Wooffice Inc.

   

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

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

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