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 the ABCs_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 your Sentences
class, outside of any particular method (Sub or Function). This declaration will look something like this:
Private Sentence As String
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."
For further reading, also see these related articles on MSDN:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…