在VB中,属性是可以有参数的,而VBA中属性使用参数非常常见。比如最常用的:Worksheet.Range("A1:A10")

 VB的语法,使用参数的不一定是方法,也有可能是属性!(虽然属性的本质是方法)

例一:参数当作“索引”使用

定义一个类模块,模块名称Ints。为简化模型,使用了只读属性。

 Private arr() As Integer

 Public Property Get ArrValue(Index As Integer) As Integer
ArrValue = arr(Index)
End Property '初始化arr(3)的值
Private Sub Class_Initialize()
arr() =
arr() =
arr() =
arr() =
End Sub

调用:

 Sub Test()
Dim c As New Ints
Debug.Print c.ArrValue() 'ArrValue是属性,并且带有参数( 索引 )
'输出结果=3
End Sub

例2:可选参数
定义一个类模块,模块名称MyCal。

这个类的作用是计算两个数的和,当加数为负数时,使加数=0 (示例使用,没多少实际意义)

 Private m As Integer
Private n As Integer Public Property Get intm() As Integer
intm = m
End Property Public Property Let intm(ByVal xvalue As Integer)
m = xvalue
End Property Public Property Get intn(Optional b As Boolean = False) As Integer
intn = n
End Property '加数为负数时,n赋值为0
Public Property Let intn(Optional b As Boolean = False, ByVal xvalue As Integer)
If b And n <= Then
n =
Else
n = xvalue
End If
End Property '计算两个数的和
Public Function MySum() As Integer
MySum = intm + intn
End Function

调用:

 Sub Test()
Dim c As New MyCal c.intm =
c.intn = - Debug.Print c.MySum '输出 0 c.intm =
c.intn(True) = - Debug.Print c.MySum '输出 4 End Sub

VBA中Range对象的Value就是有可选参数的属性

而Range对象的另外一个属性Value2是非参数化的属性

Value属性参数的意义:

最新文章

  1. SQL Server 2012安装图文教程
  2. MySQL备份还原&mdash;&mdash;AutoMySQLBackup介绍
  3. SUID, SGID, SBIT
  4. .Net Mail SMTP 发送网络邮件
  5. Max Subsequence
  6. HTML5自学笔记[ 16 ]canvas绘图基础3
  7. Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)
  8. SQL表自连接用法
  9. VS2013控制台一闪而过问题解决方法
  10. my.cnf 中字符集设置
  11. ECharts 是一款开源
  12. 一个读取C#特性Description方法
  13. 推荐——基于python
  14. [SCOI 2016]背单词
  15. spring框架学习之--数据库操作增删改查
  16. Winform开发框架之字段权限控制
  17. SQLSERVER性能调优小技巧
  18. 【机器学习】激活函数(Activation Function)
  19. 09: TemplateView , ListView ,DetailView三种常用类视图用法
  20. WebService-01-使用jdk发布第一个WebService服务并调用

热门文章

  1. SpringMVC常用配置-Controller返回格式化数据如JSON、XML的配置方式和机制
  2. 使用CXF框架集成Spring实现SOAP Web Service
  3. log4net 按时间输出日志
  4. 流量三角形:并非简单的&quot;统计学&quot;
  5. IIS下Asp.Net应用程序多进程设置及Session共享
  6. 2-MySQL数据库编码uft-8
  7. 列表边框column-rule
  8. STL—Vector简介
  9. Material Design Lite,简洁惊艳的前端工具箱 之 交互组件。
  10. 对于前端JS、Html、CSS的大小、位置是否影响网站的相应时间