1

好吧,我正在玩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)
4

2 回答 2

5

UACLevel_LevelIf在块内声明。在代码块内声明的变量仅在该代码块中可见。

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

UACLevel_Level在块外声明If,只在块内给它赋值If

请参阅Visual Basic 中的范围以供将来参考。

于 2012-08-31T07:22:35.540 回答
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
于 2012-08-31T09:21:13.900 回答