无法从另一个类访问Enum

时间:2013-07-03 18:11:01

标签: asp.net vb.net enums

我正在尝试使用实现方法this在VB.NET中实现here。我在这一行上收到错误:

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()

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

1 个答案:

答案 0 :(得分:1)

您无法访问此功能

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

将'CheckStrength'功能定义为'共享':

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

编辑:我建议您将Enum'ComminageScore'移到课程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
相关问题