[vb 2012] protection level error

🚫
Exiled
Joined
Jan 3, 2007
Messages
113
Reaction score
3
'document' is not declared. It may be inaccessible due to its protection level.

Code:
Imports System.IO
Imports System.Xml


Public Class Form1


    Public Sub Load_Click(sender As Object, e As EventArgs) Handles Start.Click
        If (System.IO.File.Exists("C:\cldata.xml")) Then
            Dim document As XmlReader = New XmlTextReader("C:\cldata.xml")
            Dim type = document.NodeType


        Else


            MessageBox.Show("The filename you selected was not found.")




        End If
    End Sub


    Public Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged


        ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)






        Dim nodes As XmlNodeList = document.SelectNodes("clanners/exlie")


        For Each node As XmlNode In nodes
            Dim name As String = node.SelectSingleNode("name").InnerText
            Dim clan As String = node.SelectSingleNode("clan").InnerText
            Dim profession As String = node.SelectSingleNode("profession").InnerText
            Dim population As String = node.SelectSingleNode("population").InnerText
            For cnt As Integer = (population) To ListView1.Columns.Count


            Next
            ListView1.Items.Add(New ListViewItem(New String() {name(0), profession(1), clan(2)}))
        Next


    End Sub


    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        Dim type = document.NodeType


        If (document.Name = "news") Then


            news.Visible = True
            news.Text = document.ToString()


        End If


        If (document.Name = "population") Then


            play.Visible = True
            play.Text = document.ToString()




        End If
    End Sub
End Class

i found this, but firstchild doesnt seem to be in the system....
http://support.microsoft.com/kb/317169

can any one yell what im doing wrong?
 
Last edited:
Its exactly what the error says; you're using document.Name without redeclaring document. You can't normally use variables or objects declared within another function, even in the same class, in the way you're trying to do it.

You need to declare your variable within the class, not within the function. Functions can modify class variables/objects which may then be used by other functions, but you can not access a variable declared in another function from within a different function.

I'm not sure on the syntax for VB but you need to go over variable and object scope within classes, or rather OOP in VB as a whole, as scope is something that is covered very early. Very early meaning within your first week of learning the language.
 
you declare document in your Load_Click sub routine:
Code:
' ...
Public Class Form1


    Public Sub Load_Click(sender As Object, e As EventArgs) Handles Start.Click
        If (System.IO.File.Exists("C:\cldata.xml")) Then
            Dim document As XmlReader = New XmlTextReader("C:\cldata.xml")
' ...

so it is only accessible in that routine. To declare a variable in your whole form, you should declare it as field below the Plublic Class Form1, so


Code:
' ...
Public Class Form1

    Dim document As XmlReader

    Public Sub Load_Click(sender As Object, e As EventArgs) Handles Start.Click
        If (System.IO.File.Exists("C:\cldata.xml")) Then
            document = New XmlTextReader("C:\cldata.xml")
' ...

Off topic: VB.NET syntax is worse, you should go for C#. ;)
 
^ theres the correct syntax for you.

Just remember to declare objects as a class object rather than within the function (sub-routine).

Off-topic; VB.net's syntax is ridiculous. At least learn c#, makes more sense and closer relates to other languages, plus the scope and flow is nearly identical with both being based on the .NET framework. :)
 
nvm i started again, all works now, thanks any ways.
 
Last edited:
Back