2013-07-03 38 views
0

我在VB.NET中實現this,試圖使用實現方法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() 

它說它沒有被定義。有什麼理由不讓我訪問它?

+0

是在同一個命名空間中的一切嗎? –

+0

@MikeCheel我相信是這樣的,我在代碼中的其他地方調用Master的函數,並且它們工作正常。看起來,調用Enum就是問題。 – Dan

回答

1

您不能訪問功能這樣

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

定義 'CheckStrength' 功能 '共享':

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

編輯:我建議你移動的枚舉'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 
+0

好的,我改變了,但我仍然收到Master.PasswordScore未定義的錯誤。 – Dan

+0

@Dan Ok,小心我的'編輯'。這應該做的伎倆。 –

+0

原來我在主類之外,但是函數看不到它。 – Dan