VB 2010“变量”没有声明。由于其保护级别,它可能无法访问
我对 VB 有点陌生,想知道如何使变量在多个 Sub 中可用。这只是一个熟悉 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
我的错误是:
“句子”未声明。由于其保护级别,它可能无法访问。”
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
My error is:
"Sentences" is not declared. It may be in accessable due to it's protection level."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
VB.NET 中的变量有一个非常特殊的范围,限制了它们的可用性代码的各个部分取决于它们的声明方式和位置。
您的
Sentence
变量具有过程级作用域,这意味着它仅在声明它的过程中可用。在您的情况下,它是在中声明的ABCs_Load
方法(“Sub”),因此它仅适用于该方法中的代码。相反,如果您希望能够访问类中任何方法中的
Sentence
变量(Forms
始终是VB中的类) .NET),您可以使用模块级作用域声明变量。为此,您需要将私有字段添加到您的Sentences
类,任何特定方法(Sub 或 Function)的外部。该声明将如下所示:当然,您也可以将变量声明为
Public
而不是Private
,这将使其可供当前类之外的其他类使用。例如,如果您有一个第二表单,您希望能够访问Sentence
变量的内容,则可以将其声明为Public
在第一个表单的类中,然后从第二表单类中的一个方法访问它,如下所示:请注意,因为它确实位于另一个表单中(与正在访问的类不同的类)在)中,您必须完全限定对它的引用。这就像你的家人可能会叫你“迈克”,但其他人必须叫你“迈克·琼斯”才能将你与“迈克·史密斯”区分开来。
如需进一步阅读,另请参阅 MSDN 上的相关文章:
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.
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 theABCs_Load
method ("Sub"), so it will only be available to code within that method.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 yourSentences
class, outside of any particular method (Sub or Function). This declaration will look something like this:Of course, you can also declare the variable as
Public
instead ofPrivate
, 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 yourSentence
variable, you could declare it asPublic
in the first form's class and then access it from one of the methods in the second form's class like so: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:
您应该将:
放在公共类句子下
阅读此内容以了解更多信息:http://msdn.microsoft.com/en-us/library/43s90322%28v=VS.80%29.aspx
You should put :
under Public Class Sentences
Read this to learn more : http://msdn.microsoft.com/en-us/library/43s90322%28v=VS.80%29.aspx
将行
Dim Sentence As String
从 ABCs_Load 移至紧接在Public Class Sentences
之后。这将使变量 Sentence 可用于 Sentences 类中的所有子函数和函数。
Move the line
Dim Sentence As String
from ABCs_Load to immediately afterPublic Class Sentences
.This will make the variable Sentence available to all subs and functions in the class Sentences.
如果页面上的每个 Web 控件都出现此情况,请右键单击出现错误的项目或文件夹,并“转换为 WebApplication” 自动生成其 Designer.vb 文件(其中它们在同名的分部类中声明)。
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).
你应该将其声明为公共变量
public Sentence as string=string.empty
但如果是你我就会在全班宣布
样本
you should declare it as a public variable
public sentence as string=string.empty
but if were you i would just declare it in the whole class
sample
将
SentenceBox.Text = Sentence
放在 End select 之后,这将解决问题。它不允许您这样做,因为 Button3 中未定义Sentence
:) 希望这会有所帮助。Put
SentenceBox.Text = Sentence
right after End select, that will fix the problem. It wasn't letting you becauseSentence
wasn't defined in Button3 :) hope this helps.将其放在“公共类句子”下:
并从 ABCs_Load 范围中删除该声明。
Put this under "Public Class Sentences":
And remove the declaration from the ABCs_Load scope.