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

北慕城南 提交于 2020-01-10 04:23:20

问题


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


回答1:


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:

  • Access Levels in Visual Basic
  • How to: Control the Scope of a Variable (Visual Basic)



回答2:


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




回答3:


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

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




回答4:


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




回答5:


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

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:


Put this under "Public Class Sentences":

Dim Sentence As String = String.Empty

And remove the declaration from the ABCs_Load scope.



来源:https://stackoverflow.com/questions/4289426/vb-2010-variable-is-not-declared-it-may-be-inaccessible-due-to-its-protectio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!