未检测到变量被声明

时间:2012-08-31 07:15:06

标签: vb.net variable-declaration

好吧,我正在玩Visual Basic,似乎有一段时间开始使用xD。无论如何不确定为什么我收到以下错误:

  

UACLevel_Level未声明。由于其保护级别,它可能无法访问。

我尝试点击小帮助图标的东西,它什么都没给我。

Dim ConsentPromptBehaviorAdmin = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "ConsentPromptBehaviorAdmin", Nothing)
Dim EnableLUA = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "EnableLUA", Nothing)
Dim PromptOnSecureDesktop = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "PromptOnSecureDesktop", Nothing)
Dim UACLevel_Value = ConsentPromptBehaviorAdmin + EnableLUA + PromptOnSecureDesktop
If UACLevel_Value = 0 Then
    Dim UACLevel_Level = "Never notify me."
ElseIf UACLevel_Value = 6 Then
    Dim UACLevel_Level = "Notify me only when programs try to make changes to my computer(do not dim desktop)."
ElseIf UACLevel_Value = 7 Then
    Dim UACLevel_Level = "Default - Notify me only when programs try to make changes to my computer."
ElseIf UACLevel_Value = 4 Then
    Dim UACLevel_Level = "Always Notify Me"
Else
    Dim UACLevel_Level = "Customized UAC Level"
End If
MsgBox("UACLevel is " & UACLevel_Value & ": " & UACLevel_Level)

2 个答案:

答案 0 :(得分:5)

UACLevel_LevelIf块内声明。在代码块内声明的变量只能从该块中看到。

这与VB6 / VBA不同,它在块外可见(此时你会得到一个多重声明错误,因为你声明了五次)。

UACLevel_Level块之外声明If,并仅在If块中为其指定值。

请参阅Scope in Visual Basic以供将来参考。

答案 1 :(得分:0)

对于任何感兴趣的人,最终结果如下。

Module Module1

    Sub Main()
        Dim ConsentPromptBehaviorAdmin = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "ConsentPromptBehaviorAdmin", Nothing)
        Dim EnableLUA = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "EnableLUA", Nothing)
        Dim PromptOnSecureDesktop = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", "PromptOnSecureDesktop", Nothing)
        Dim UACLevel_Value = ConsentPromptBehaviorAdmin + EnableLUA + PromptOnSecureDesktop
        Dim UACLevel_level As String
        If UACLevel_Value = 0 Then
            UACLevel_level = "Never notify me."
        ElseIf UACLevel_Value = 6 Then
            UACLevel_level = "Notify me only when programs try to make changes to my computer(do not dim desktop)."
        ElseIf UACLevel_Value = 7 Then
            UACLevel_level = "Default - Notify me only when programs try to make changes to my computer."
        ElseIf UACLevel_Value = 4 Then
            UACLevel_level = "Always Notify Me"
        Else
            UACLevel_level = "Customized UAC Level"
        End If
        MsgBox("UACLevel is " & UACLevel_Value & ": " & UACLevel_Level)

    End Sub

End Module