locked
Variable not declared? RRS feed

  • Question

  • Hi all: I'm trying to write a text file editing utility for my work as a surveyor. Trying to start at a very elementary level (like open and read a file)

    In the code window: The line after the Dim statement has StreamToDisplay underlined w/ a blue squiggly (declaration expected). I thought it was declared.

    TextBox1 is similarly underlined. I don't get that at all. 'Declaration expected' for a text box?

    The code as written is:

    Imports System.IO

    Public Class Form1
        Dim StreamToDisplay As StreamReader
        StreamToDisplay = New SteamReader("c:\Survey Data\level.txt")
        TextBox1.Text = StreamToDisplay.ReadToEnd
        StreamToDisplay.Close()
    End Class

    What am I doing wrong? There must be something very elementary I'm not getting yet. Thanks

    Sunday, November 25, 2012 9:10 PM

Answers

  • You cannot include executable code at the form level.  Put that code (after the declaration) into a method, such as the form load method, or a button click method, or whatever should start that procedure off.

    • Proposed as answer by Mark Liu-lxf Monday, November 26, 2012 7:22 AM
    • Marked as answer by Mark Liu-lxf Wednesday, December 5, 2012 6:37 AM
    Sunday, November 25, 2012 9:19 PM

All replies

  • You cannot include executable code at the form level.  Put that code (after the declaration) into a method, such as the form load method, or a button click method, or whatever should start that procedure off.

    • Proposed as answer by Mark Liu-lxf Monday, November 26, 2012 7:22 AM
    • Marked as answer by Mark Liu-lxf Wednesday, December 5, 2012 6:37 AM
    Sunday, November 25, 2012 9:19 PM
  • re:

    "TextBox1 is similarly underlined. I don't get that at all. 'Declaration expected' for a text box?"

    The compiler is saying that TextBox1 is undeclared. It is saying that it expects to see a declaration (of an executable method) at that point in the sourcecode.


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Sunday, November 25, 2012 9:38 PM
  • In the designer window click on the top border of the form and then the textbox and it should create code that has the following Submit routines in it. You were trying to declare three of your statements as declarations I think. Anyhow this works on my P/C.

    Imports System.IO
    
    Public Class Form1
    
        Dim StreamToDisplay As StreamReader
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            StreamToDisplay = New StreamReader("C:\Users\John\Desktop\Unicode Info.txt")
            TextBox1.Text = StreamToDisplay.ReadToEnd
            StreamToDisplay.Close()
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    End Class


    You've taught me everything I know but not everything you know.

    Sunday, November 25, 2012 9:40 PM
  • If it is saying that when the code is at the form level, then the reason is the same.

    If it is saying that when you have moved the code into a method then it is becausue there is no variable named "TextBox1" declared in your applciation. You should add a text box control using the designer - a variable will be declared for you.  A variable that refers to a control such as a text box needs to be declared like any other variable. 

    Sunday, November 25, 2012 10:04 PM
  • Button_click.  Event procedure. Must have read it 5 times. Thank you Acamar
    Sunday, November 25, 2012 11:57 PM
  • What I meant to say above:

    re:

    "TextBox1 is similarly underlined. I don't get that at all. 'Declaration expected' for a text box?"

    The compiler is [edit] NOT saying that TextBox1 is undeclared. It is saying that it expects to see a declaration (of an executable method) at that point in the sourcecode.


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Monday, November 26, 2012 12:03 AM
  • Hello,

    If you would like to simply open a file it only takes one line of code.

    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
    Handles Button1.Click
        TextBox1.Text = IO.File.ReadAllText("c:\Survey Data\level.txt")
    End Sub

    Same for writing, one line of code.

    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
    Handles Button1.Click
        IO.File.WriteAllText("c:\Survey Data\level.txt", TextBox1.Text)
    End Sub


    KSG

    Monday, November 26, 2012 3:05 AM