感觉好玩但不一定好用的代码
类CLink代码如下:
判断是否包含字符串的一个用法,下边只是一个范例:
Private mI As Integer
Public Function Append(i As Integer) As CLink
mI = mI + i
Set Append = Me
End Function
Public Function GetValue() As Integer
GetValue = mI
End Function
用法如下:Public Function Append(i As Integer) As CLink
mI = mI + i
Set Append = Me
End Function
Public Function GetValue() As Integer
GetValue = mI
End Function
Dim c As CLink
Set c = New CLink
MsgBox c.Append(1).Append(2).Append(3).GetValue
Set c = Nothing
好玩的就是Append函数的用法,《Programming Ruby》里边有一段这么的用法,只是感觉有趣。Set c = New CLink
MsgBox c.Append(1).Append(2).Append(3).GetValue
Set c = Nothing
判断是否包含字符串的一个用法,下边只是一个范例:
Private Function IncludeNumber(s As String) As Boolean
Static c As Collection
Dim i As Integer
Dim v As String
If c Is Nothing Then
Set c = New Collection
For i = 0 To 9
c.Add i, "c" & i
Next
End If
On Error GoTo e:
c.Item "c" & s
IncludeNumber = True
Exit Function
e:
End Function
说白了,很多时候适当利用错误处理进行一些逻辑判断也是好的。
Static c As Collection
Dim i As Integer
Dim v As String
If c Is Nothing Then
Set c = New Collection
For i = 0 To 9
c.Add i, "c" & i
Next
End If
On Error GoTo e:
c.Item "c" & s
IncludeNumber = True
Exit Function
e:
End Function