VB 2010 '变量' 未声明.由于它的保护级别,它可能无法访问 [英] VB 2010 'variable' is not declared. It may be inaccessible due to it's protection level

查看:37
本文介绍了VB 2010 '变量' 未声明.由于它的保护级别,它可能无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm sort of a n00b to VB and was wondering how to make a variable available across multiple Subs. It's just a test app to get familiar with VB. My Code:

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

我的错误是:

未声明句子".由于其保护级别,它可能处于可访问状态."

"Sentences" is not declared. It may be in accessable due to it's protection level."

推荐答案

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

Variables in VB.NET have a very particular scope, limiting their availability to various portions of your code depending on how and where they are declared.

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

Your Sentence variable has procedure-level scope, which means that it is available only within the procedure in which it was declared. In your case, it's declared in the ABCs_Load method ("Sub"), so it will only be available to code within that method.

相反,如果您希望能够访问任何类中的方法中的Sentence 变量(Forms 总是VB.NET 中的类),您可以声明具有模块级作用域的变量.为此,您需要在您的 Sentences 类,外部任何特定方法(Sub 或 Function).这个声明看起来像这样:

If, instead, you want to be able to access the Sentence variable in any of the methods in your class (Forms are always classes in VB.NET), you can declare the variable with Module-level scope. To do this, you need to add a private field to your Sentences class, outside of any particular method (Sub or Function). This declaration will look something like this:

Private Sentence As String


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


Of course, you can also declare the variable as Public instead of Private, which will make it available to other classes outside of the current class. For example, if you had a second form that you wanted to be able to access the contents of your Sentence variable, you could declare it as Public in the first form's class and then access it from one of the methods in the second form's class like so:

MessageBox.Show(myForm1.Sentence)

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

Notice that because it does lie within another form (a class different than the one it is being accessed in), you have to fully qualify the reference to it. It's like how your family might call you "Mike," but others have to call you "Mike Jones" to differentiate you from "Mike Smith."


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


For further reading, also see these related articles on MSDN:

这篇关于VB 2010 '变量' 未声明.由于它的保护级别,它可能无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆