VB 2010 "variable" is not declared. It may not be available due to its level of protection.

I kind of like n00b for VB and wondered how to make the variable available through multiple Subs. This is just a test application to get to know VB. My code is:

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 mistake:

"Offers" are not announced. It may be available due to its level of protection.

+3
source share
6 answers

Variables in VB.NET have a very specific area of limiting their accessibility to various parts of your code, depending on how and where they are declared.

Sentence , , , . ​​ ABCs_Load ( "Sub" ), .

Sentence (Forms VB.NET), , Sentences (Sub Function). :

Private Sentence As String


, Public Private, . , , Sentence, Public , :

MessageBox.Show(myForm1.Sentence)

, (, , ), . , "", " ", " ".


. MSDN:

+6
+2

Dim Sentence As String ABCs_Load Public Class Sentences.

Sentence .

0

- , " WebApplication" , designer.vb( ).

0

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
Hide result
0

"Public Class Sentences":

Dim Sentence As String = String.Empty

ABCs_Load.

-1

Source: https://habr.com/ru/post/1777134/


All Articles