vb.net VB 2010 '变量' 未声明。由于它的保护级别,它可能无法访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:10:03  来源:igfitidea点击:

VB 2010 'variable' is not declared. It may be inaccessible due to it's protection level

vb.netvariablesscopedeclaration

提问by benj

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:

我有点像 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

My error is:

我的错误是:

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

“句子”没有声明。由于它的保护级别,它可能是可访问的。”

回答by Cody Gray

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.

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

Your Sentencevariable 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_Loadmethod ("Sub"), so it will only be available to code within that method.

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

If, instead, you want to be able to access the Sentencevariable in anyof the methods in your class (Formsare always classes in VB.NET), you can declare the variable with Module-level scope. To do this, you need to add a private fieldto your Sentencesclass, outsideof any particular method (Sub or Function). This declaration will look something like this:

相反,如果您希望能够在您的类Sentence中的任何方法中访问该变量(Forms在 VB.NET 中始终是类),您可以使用模块级作用域声明该变量。要做到这一点,你需要添加一个私有字段到你的Sentences课,外面任何特定的方法(Sub或Function)的。这个声明看起来像这样:

Private Sentence As String


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


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

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."

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


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


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

回答by AFract

You should put :

你应该把:

Private Sentence As String

under Public Class Sentences

在公共类句子下

Read this to learn more : http://msdn.microsoft.com/en-us/library/43s90322%28v=VS.80%29.aspx

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

回答by xpda

Move the line Dim Sentence As Stringfrom ABCs_Load to immediately after Public Class Sentences.

将行Dim Sentence As String从 ABCs_Load移动到Public Class Sentences.

This will make the variable Sentence available to all subs and functions in the class Sentences.

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

回答by hagensoft

If you get this for every webcontrol on page then right Click on the project or folder with error and 'Convert to WebApplication'to auto-generate its designer.vb files (where they get declared in a partial class with the same name).

如果页面上的每个 webcontrol 都得到了这个,那么右键单击有错误的项目或文件夹,然后“转换为 WebApplication”以自动生成它的 Designer.vb 文件(它们在具有相同名称的部分类中声明)。

回答by popopopoot

you should declare it as a public variable public sentence as string=string.emptybut if were you i would just declare it in the whole class sample

你应该将它声明为一个公共变量,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

回答by RobinJ

Put this under "Public Class Sentences":

把它放在“公共类句子”下:

Dim Sentence As String = String.Empty

And remove the declaration from the ABCs_Load scope.

并从 ABCs_Load 范围中删除声明。