【问题标题】:VB 2010 'variable' is not declared. It may be inaccessible due to it's protection level未声明 VB 2010 '变量'。由于它的保护级别,它可能无法访问
【发布时间】:2010-11-27 00:32:04
【问题描述】:

我是 VB 的一个 n00b 并且想知道如何使一个变量在多个 Subs 中可用。它只是一个熟悉VB的测试应用程序。 我的代码:

Public Class Sentences

Private Sub SentenceBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SentenceBox.TextChanged
    If Me.Text = Trim(Sentence) Then
        MsgBox("Good job!")
        Main_Menu.Show()
        Me.Close()
    End If
End Sub

Private Sub ABCs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim random As Integer = CInt((Rnd() * 10) + 1)
    Dim Sentence As String


    Select Case random
        Case 1
            Sentence = "The quick brown fox jumped over the lazy dog!"
        Case 2
            Sentence = "Hi there, how are you doing?"
        Case 3
            Sentence = "What is the answer to life?"
        Case 4
            Sentence = "The cat in the hat was fat."
        Case 5
            Sentence = "John and Sam had always been fat."
        Case 6
            Sentence = "The snow is falling hard."
        Case 7
            Sentence = "Here, dinner is always served nightly."
        Case 8
            Sentence = "The dog barks at the passing cars."
        Case 9
            Sentence = "The dust settles on the books."
        Case 10
            Sentence = "Fire burns brightly when you add kerosene."
    End Select
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    SentenceBox.Text = Sentence

    End Sub
End Class

我的错误是:

“句子”未声明。由于它的保护级别,它可能处于可访问状态。”

【问题讨论】:

  • 我认为您没有发布实际产生此错误消息的代码。

标签: vb.net variables scope declaration


【解决方案1】:

VB.NET 中的变量有一个非常特殊的scope,根据声明它们的方式和位置,限制它们在代码的各个部分中的可用性。

您的 Sentence 变量具有过程级范围,这意味着它仅在声明它的过程中可用。在您的情况下,它是在 ABCs_Load 方法中声明的( "Sub"),因此它只能用于该方法中的代码。

相反,如果您希望能够访问类中任何方法中的Sentence 变量(Forms 始终是 VB.NET 中的类),您可以声明具有模块级范围的变量。为此,您需要将private field 添加到您的Sentences 类中,在任何特定方法(Sub 或Function)之外。该声明将如下所示:

Private Sentence As String


当然,您也可以将变量声明为Public 而不是Private,这将使其可用于当前类之外的其他类。例如,如果您有一个 second 表单,您希望能够访问您的 Sentence 变量的内容,您可以在第一个表单的类中将其声明为 Public,然后访问它从 second 表单的类中的一种方法,如下所示:

MessageBox.Show(myForm1.Sentence)

请注意,因为它确实位于另一种形式中(一个不同于访问它的类),所以您必须完全限定对它的引用。这就像您的家人可能会称您为“迈克”,但其他人必须称您为“迈克琼斯”以区分您与“迈克史密斯”。


如需进一步阅读,另请参阅 MSDN 上的这些相关文章:

【讨论】:

    【解决方案2】:

    你应该把:

    Private Sentence As String
    

    在公共课句下

    阅读本文以了解更多信息:http://msdn.microsoft.com/en-us/library/43s90322%28v=VS.80%29.aspx

    【讨论】:

      【解决方案3】:

      Dim Sentence As String 行从ABCs_Load 移到Public Class Sentences 之后。

      这将使变量 Sentence 可用于 Sentences 类中的所有子和函数。

      【讨论】:

        【解决方案4】:

        如果您为页面上的每个 web 控件获取此信息,则右键单击出现错误的项目或文件夹并 '转换为 WebApplication' 以自动生成其设计器.vb文件(它们在同名的部分类中声明)。

        【讨论】:

          【解决方案5】:

          您应该将其声明为公共变量public sentence as string=string.empty 但如果你是我,我会在全班宣布 样本

          public class NameOfClass
            dim sentence as string=string.empty
          
            public sub nameOfSub
              --you can use the variable 'sentence' here
            end sub
            public sub nameOfSub2
              --you can use the variable 'sentence' here
            end sub
          end class

          【讨论】:

            【解决方案6】:

            SentenceBox.Text = Sentence 放在 End select 之后,这将解决问题。它没有让你,因为 Sentence 没有在 Button3 中定义 :) 希望这会有所帮助。

            【讨论】:

              【解决方案7】:

              把这个放在“公共课句”下面:

              Dim Sentence As String = String.Empty
              

              并从 ABCs_Load 范围中删除声明。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-01-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多