0

我通过尝试使用此处的实现方法在 VB.NET 中实现一点。我在这一行遇到错误:

Dim score As Master.PasswordScore = Master.CheckStrength(txtPassVal)

错误是:

 Type 'Master.PasswordScore' is not defined.

以下是相关的完整代码:

硕士课:

Public Enum PasswordScore
    Blank = 0
    TooShort = 1
    RequirementsNotMet = 2
    VeryWeak = 3
    Weak = 4
    Fair = 5
    Medium = 6
    Strong = 7
    VeryStrong = 8
End Enum

Public Function CheckStrength(ByVal password As String) As PasswordScore
    Dim score As Int32 = 0

    If password.Length < 1 Then
        Return PasswordScore.Blank
    End If

    If password.Length < 8 Then
        Return PasswordScore.TooShort
    End If

    If password.Contains("password") Or password.Contains("12345678") Then
        Return PasswordScore.RequirementsNotMet
    End If

    If password.Length >= 8 Then
        score += 2
    End If

    If password.Length >= 12 Then
        score += 1
    End If

    If Regex.IsMatch(password, "/\d+/", RegexOptions.ECMAScript) Then
        'Contains a number
        score += 1
    End If

    If Regex.IsMatch(password, "/[a-z]/", RegexOptions.ECMAScript) Then
        'Contains a lowercase letter
        score += 1
    End If

    If Regex.IsMatch(password, "/[A-Z]/", RegexOptions.ECMAScript) Then
        'Contains an uppercase letter
        score += 1
    End If

    If Regex.IsMatch(password, "/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript) Then
        'Contains special character
        score += 2
    End If

    Return CType(score, PasswordScore)
End Function

在另一个类中使用代码:

    Dim score As Master.PasswordScore = Master.CheckStrength(txtPassVal)
    Dim i As Int32 = CType(score, Int32)

    If i < 4 Then
        lblMsg.Text = "Password does not meet minimum security requirements."
        lblPasswordStrength.ForeColor = Drawing.Color.Red
        Exit Sub
    ElseIf i > 3 Then
        lblPasswordStrength.ForeColor = Drawing.Color.DarkGreen
    End If
    lblPasswordStrength.Text = score.ToString()

它说它没有定义。有什么理由不让我访问它吗?

4

1 回答 1

1

您无法访问这样的功能

Dim score As Master.PasswordScore = Master.CheckStrength(txtPassVal)

将“CheckStrength”函数定义为“Shared”:

Public Shared Function CheckStrength(ByVal password As String) As PasswordScore

编辑:我建议你将 Enum 'PasswordScore' 移到课堂之外Master,然后像这样访问它:

 Dim score As PasswordScore = Master.CheckStrength(txtPassVal)

编辑2:

Public Class Master
Public Shared Function CheckStrength(ByVal password As String) As PasswordScore
    Dim score As Int32 = 0

    If password.Length < 1 Then
        Return PasswordScore.Blank
    End If

    If password.Length < 8 Then
        Return PasswordScore.TooShort
    End If

    If password.Contains("password") Or password.Contains("12345678") Then
        Return PasswordScore.RequirementsNotMet
    End If

    If password.Length >= 8 Then
        score += 2
    End If

    If password.Length >= 12 Then
        score += 1
    End If

    If Regex.IsMatch(password, "/\d+/", RegexOptions.ECMAScript) Then
        'Contains a number
        score += 1
    End If

    If Regex.IsMatch(password, "/[a-z]/", RegexOptions.ECMAScript) Then
        'Contains a lowercase letter
        score += 1
    End If

    If Regex.IsMatch(password, "/[A-Z]/", RegexOptions.ECMAScript) Then
        'Contains an uppercase letter
        score += 1
    End If

    If Regex.IsMatch(password, "/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript) Then
        'Contains special character
        score += 2
    End If

    Return CType(score, PasswordScore)
End Function
End Class

Public Enum PasswordScore
    Blank = 0
    TooShort = 1
    RequirementsNotMet = 2
    VeryWeak = 3
    Weak = 4
    Fair = 5
    Medium = 6
    Strong = 7
    VeryStrong = 8
End Enum
于 2013-07-03T18:24:55.470 回答