VB.NET的隐藏特性?

我已经通过C#的隐藏特性了解了相当多的浏览内容,并且当我无法找到类似于VB.NET的东西时感到惊讶。

那么它的一些隐藏或较少已知的功能是什么?


子句时的Exception When很大程度上是未知的。

考虑这个:

Public Sub Login(host as string, user as String, password as string, _
                            Optional bRetry as Boolean = False)
Try
   ssh.Connect(host, user, password)
Catch ex as TimeoutException When Not bRetry
   ''//Try again, but only once.
   Login(host, user, password, True)
Catch ex as TimeoutException
   ''//Log exception
End Try
End Sub

自定义Enum

VB的真正隐藏功能之一是completionlist XML文档标记,可用于创建具有扩展功能的自己的Enum类型的类型。 但是,该功能在C#中无法使用。

我最近的一个代码的一个例子是:

'
''' <completionlist cref="RuleTemplates"/>
Public Class Rule
    Private ReadOnly m_Expression As String
    Private ReadOnly m_Options As RegexOptions

    Public Sub New(ByVal expression As String)
        Me.New(expression, RegexOptions.None)
    End Sub

    Public Sub New(ByVal expression As String, ByVal options As RegexOptions)
        m_Expression = expression
        m_options = options
    End Sub

    Public ReadOnly Property Expression() As String
        Get
            Return m_Expression
        End Get
    End Property

    Public ReadOnly Property Options() As RegexOptions
        Get
            Return m_Options
        End Get
    End Property
End Class

Public NotInheritable Class RuleTemplates
    Public Shared ReadOnly Whitespace As New Rule("s+")
    Public Shared ReadOnly Identifier As New Rule("w+")
    Public Shared ReadOnly [String] As New Rule("""([^""]|"""")*""")
End Class

现在,当为一个声明为Rule的变量赋值时,IDE提供一个来自RuleTemplates的可能值的IntelliSense列表。

/编辑:

由于这是一个依赖于IDE的功能,因此很难在使用它时显示它的外观,但我只会使用屏幕截图:

实际完成列表http://page.mi.fu-berlin.de/krudolph/stuff/completionlist.png

实际上,智能感知与使用Enum时获得的100%完全相同。


你有没有注意到Like比较运算符?

Dim b As Boolean = "file.txt" Like "*.txt"

更多来自MSDN

Dim testCheck As Boolean

' The following statement returns True (does "F" satisfy "F"?)'
testCheck = "F" Like "F"

' The following statement returns False for Option Compare Binary'
'    and True for Option Compare Text (does "F" satisfy "f"?)'
testCheck = "F" Like "f"

' The following statement returns False (does "F" satisfy "FFF"?)'
testCheck = "F" Like "FFF"

' The following statement returns True (does "aBBBa" have an "a" at the'
'    beginning, an "a" at the end, and any number of characters in '
'    between?)'
testCheck = "aBBBa" Like "a*a"

' The following statement returns True (does "F" occur in the set of'
'    characters from "A" through "Z"?)'
testCheck = "F" Like "[A-Z]"

' The following statement returns False (does "F" NOT occur in the '
'    set of characters from "A" through "Z"?)'
testCheck = "F" Like "[!A-Z]"

' The following statement returns True (does "a2a" begin and end with'
'    an "a" and have any single-digit number in between?)'
testCheck = "a2a" Like "a#a"

' The following statement returns True (does "aM5b" begin with an "a",'
'    followed by any character from the set "L" through "P", followed'
'    by any single-digit number, and end with any character NOT in'
'    the character set "c" through "e"?)'
testCheck = "aM5b" Like "a[L-P]#[!c-e]"

' The following statement returns True (does "BAT123khg" begin with a'
'    "B", followed by any single character, followed by a "T", and end'
'    with zero or more characters of any type?)'
testCheck = "BAT123khg" Like "B?T*"

' The following statement returns False (does "CAT123khg" begin with'
'    a "B", followed by any single character, followed by a "T", and'
'    end with zero or more characters of any type?)'
testCheck = "CAT123khg" Like "B?T*"
链接地址: http://www.djcxy.com/p/52951.html

上一篇: Hidden Features of VB.NET?

下一篇: What's the strangest corner case you've seen in C# or .NET?