无需继承即可扩展字符串类函数 [英] Extends Strings Class Functions without inheritance
问题描述
我正在考虑扩展 strings 函数并将我自己的函数添加到模块中.但我发现字符串类是不可继承的.
I am thinking to extends the strings functions and add my own functions to the module. but I found that the strings class is non-inheritable.
有什么方法可以扩展类.我发现了类似的东西:
is there any way to extends the class. I found something like :
Class MyStringType
Dim str As String
function toTitleCase()
'return titlecae
end function
End Class
Dim s As MyStringType
s.str = "mystringgoeshere"
但这并没有以正确的方式扩展,它会导致:
but this does not extends in the proper way, it will results to :
S.Str.normalStringFunctions
S.MyOwnFunctionsHere
结果,功能不在同一级别!!
in result, the functions are not in the same level !!
有什么想法吗?
推荐答案
你可以写 扩展方法也在VB.NET中
You can write Extension methods also in VB.NET
Module MyExtensions
<Extension()>
Public Sub toTitleCase(input As String)
....
End Sub
End Module
在这个语法糖"到位后,您可以将代码编写为
After this 'syntactic sugar' is in place you can write your code as
Dim newString = oldString.toTitleCase()
但是,我会记住您,ToTitleCase 方法已经在 System.Globalization.TextInfo 命名空间
However, I would to remember you that ToTitleCase method is already available in the System.Globalization.TextInfo namespace
这篇关于无需继承即可扩展字符串类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!