1

我有一个查询,其中一个值由 UDF 返回:

select name,coord,convertCoord(coord) from testTable;

convertCoord()使用RegexMatchCollection对象返回其值:

Dim re As New RegExp
Dim mtch As Match
Dim matches As MatchCollection

Function convertCoord(str As String) As String

re.Pattern = "(find|this)pattern"
Set matches = re.Execute(str)
If matches.Count > 0 Then
    Set mtch = matches(1)
    convertCoord = mtch.Value
Else
    convertCoord = ""
End If

End Function

我正在尝试加快查询速度,我想知道是否有办法制作 , 的一个实例remtch并且matches每次调用convertCoord(). 如果我理解正确,查询中的每个结果行都会调用convertCoord(),它会重复构造和破坏所有对象,并且所有这些对象的创建都会减慢查询速度。

还是它们已经是静态的,因此只构造了一次,因为我已经在函数之外声明了它们?

4

1 回答 1

1

声明时可以使用Static关键字RegExp。但是,您只能在过程(函数或子例程)中使用它。如果您尝试将其用于模块级变量,则会触发编译器错误。

我认为您不需要声明mtch匹配项,Static因为您不想将它们的值从一个函数调用保留到下一个函数调用。我也不明白为什么它们应该是模块级变量,所以我将它们设置为函数的本地变量。

Function convertCoord(str As String) As String
Static re As RegExp
Dim mtch As Match
Dim matches As MatchCollection

If re Is Nothing Then
    Debug.Print "RegExp Is Nothing"
    Set re = New RegExp
    re.pattern = "(find|this)pattern"
Else
    Debug.Print "RegExp active"
End If

' insert code which uses the RegExp

End Function

使用您的查询测试类似的功能。在您确认它只打印一次“RegExp Is Nothing”之后,您可能会想要丢弃这些Debug.Print语句。:-)

于 2013-02-12T05:33:36.280 回答