相当于vb.net中的“this”关键字

时间:2013-06-29 20:10:18

标签: vb.net extension-methods

请告诉我如何在vb.net中编写下一个代码?

public static bool IsYear(this string data)
    {
        return data.Length == 4 && Regex.IsMatch(data, @"[12]+[0-9]+[0-9]+[0-9]");
    }

1 个答案:

答案 0 :(得分:2)

看看这个MSDN article on VB extension Methods。应用它将导致类似的事情。

Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim myString As String
        myString = "1955"
        Console.WriteLine(myString & " is Year =" & myString.IsYear)
        myString = "3955"
        Console.WriteLine(myString & " is Year =" & myString.IsYear)
        myString = "19555"
        Console.WriteLine(myString & " is Year =" & myString.IsYear)
        Console.ReadLine()
    End Sub

End Module
Module Extensions
    <Extension()>
    Public Function IsYear(ByVal data As String) As Boolean
        Return data.Length = 4 And Regex.IsMatch(data, "[12]+[0-9]+[0-9]+[0-9]")
    End Function
End Module