I'm getting an error for using a variable before it's assigned a value, but, I'm not sure why.

Imports System.IO

Public Class Form1


    Dim colWeather As New Collection
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    'Add listbox items upon form load.
    With lstAltitudes.Items
        .Add("03000")
        .Add("06000")
        .Add("09000")
        .Add("12000")
        .Add("18000")
        .Add("24000")
        .Add("30000")
        .Add("34000")
        .Add("39000")
    End With
End Sub

Private Sub ReadStationsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadStationsToolStripMenuItem.Click
    With ofdOpenFile
        .Title = "Open Stations File"
        .InitialDirectory = Application.StartupPath
        .Filter = "Text Files (*.txt)|*.txt"
        .ShowDialog()
    End With
    If ofdOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
        cboLocations.Items.Clear()
        Dim stationsFile As StreamReader = File.OpenText("stations.txt")
        Dim strStations As String = stationsFile.ReadLine()
        Do While stationsFile.Peek >= 0
            cboLocations.Items.Add(stationsFile.ReadLine)
        Loop
    cboLocations.Enabled = True
    End If
End Sub

Private Sub ReadWeatherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadWeatherToolStripMenuItem.Click
    Dim inputFile As StreamReader 'Object Variable
    Dim strWeather As String 'Station line
    Dim strFileName As String = "weather.txt"
    Dim intCount As Integer 'Loop counter
    Dim key As Integer 'Key

    'OpenFileDialog
    With ofdOpenFile
        .Filter = "Text Files (*.txt)|*.txt" ' Opens text files only
        .InitialDirectory = Application.StartupPath() 'Initial directory
        .Title = "Open Weather File"
        .FileName = strFileName


        If .ShowDialog() = Windows.Forms.DialogResult.OK Then
            Try
                'Clears collection
                colWeather.Clear()

                'Opens the weather File
                inputFile = File.OpenText(strFileName)

                'Read each line after first seven lines
                For intCount = 8 To CInt(inputFile.Peek = -1)
                    strWeather = inputFile.ReadLine()
                Next
                inputFile.Close()

                '********ERROR IS IN THIS LINE BELOW**********
                'extract the first three characters as a key
                key = CInt((*strWeather*.Substring(1, 3)))

                'store the entire line in the collection
                colWeather.Add(inputFile.ReadLine(), CStr(key))


                'Enables listbox
                lstAltitudes.Enabled = True


            Catch
                MessageBox.Show("File Does not exist")
            End Try

        End If
    End With

End Sub

End Class

Recommended Answers

All 15 Replies

I think your problem might be here:

For intCount = 8 To CInt(inputFile.Peek = -1)
    strWeather = inputFile.ReadLine()
Next

The limit is less than the start. the statement inside won't run, therefore strweather never gets assigned.

Not sure what you expect to happen with that but inputFile.Peek = -1 is a boolean expression which means that CInt(inputFile.Peek = -1) should produce only a 0 or a 1

i was trying to highlight that item and forgot to remove it when i pasted

this might serve you better:

For I = 1 to 7
    If inputFile.Peek >=0 Then
        strWeather = inputFile.ReadLine()
    End If
Next
While inputFile.Peek >=0
    strWeather = inputFile.ReadLine()
    key = CInt(strWeather.Substring(1, 3))
    'store the entire line in the collection
    colWeather.Add(strWeather, CStr(key))
End While
    'Enables listbox
lstAltitudes.Enabled = True
inputFile.Close()   

VB will flag a problem if the variable is set inside another block. For example

Dim s As String

If x > 5 Then
    s = "bigger"
End If

Msgbox(s)

will indicate a problem because there is an execution path where s does not get assigned a value. I have had code where the variable absolutely will get assigned a value but the compiler could not figure it out. I generally do this to make the warning go away

Dim s As String = ""

If x > 5 Then
    s = "bigger"
End If

Msgbox(s)

I still get my catch message box. At this point I'm not really sure what is happening anymore. I've tried reworking it so many times. It looks good to my newb eyes, but I really have no idea what i am doing wrong.

Private Sub ReadWeatherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadWeatherToolStripMenuItem.Click
    Dim inputFile As StreamReader 'Object Variable

    With ofdOpenFile
        .Title = "Open Weather File"
        .InitialDirectory = Application.StartupPath
        .Filter = "Text Files (*.txt)|*.txt"

    End With


    Dim strFileName As String = "weather.txt"
    'OpenFileDialog
    With ofdOpenFile
        .Filter = "Text Files (*.txt)|*.txt" ' Opens text files only
        .InitialDirectory = Application.StartupPath() 'Initial directory
        .Title = "Open Weather File"
        .FileName = strFileName

        If .ShowDialog() = Windows.Forms.DialogResult.OK Then
            Try

                Dim key As Integer 'Key
                Dim strWeather As String = "" 'Station line
                'Clears collection
                colWeather.Clear()

                'Opens the weather File
                inputFile = File.OpenText(strFileName)

                'Read each line after first seven lines
                For I = 1 To 7
                    If inputFile.Peek >= 0 Then
                        strWeather = inputFile.ReadLine()
                    End If
                Next
                While inputFile.Peek >= 0
                    strWeather = inputFile.ReadLine()
                    key = CInt(strWeather.Substring(1, 3))
                    'store the entire line in the collection
                    colWeather.Add(strWeather, CStr(key))
                End While
                'Enables listbox
                lstAltitudes.Enabled = True
                inputFile.Close()


            Catch
                MessageBox.Show("File Does not exist")
            End Try

        End If
    End With

End Sub

I still get my catch message box. At this point I'm not really sure what is happening anymore...

Comment out the Try-Catch statements and let the debugger show you where the error is occuring. Inspect the variables and see if they are what you would expect.

For some guidance on debugging, read : Debugging in Visual Basic .NET. It is old but still valid.

try this:

Catch ex As Exception
    ' Show the exception's message.
    MessageBox.Show(ex.Message)

This will will give a more verbose message and make it easier to figure out what is wrong.

Alternatively comment out the Try-Catch block and let VS tell you which line is throwing the exception.

the issue is strWeather is not getting assigned a value so when I pull the index 1 in

key = CInt(strWeather.Substring(1, 3))

it get index and length must refer to location in string. I knew that, but I'm not sure why!!

Modify your code like this:

                'Read each line after first seven lines
                For I = 1 To 7
                    If inputFile.Peek >= 0 Then
                        strWeather = inputFile.ReadLine()
Stop 
                    End If
                Next
                While inputFile.Peek >= 0
                    strWeather = inputFile.ReadLine()
Stop
                    key = CInt(strWeather.Substring(1, 3))
                    'store the entire line in the collection
                    colWeather.Add(strWeather, CStr(key))
                End While

Then run it and inspect strWeather each time then code stops. Most likely you have an empty line if the text file that is being read.

Hit F5 to continue execution.

If you can modify the structure of the text file, a delimiter line, perhaps a line with ** on it, can be used to find the right data, instead of just blindly counting. something like this:

'For I = 1 To 7
'If inputFile.Peek >= 0 Then
'strWeather = inputFile.ReadLine()
'End If
'Next
While inputFile.Peek >= 0
    strWeather = inputFile.ReadLine()
    If strWeather.Contains("**") Then
        While inputFile.Peek >= 0
            strWeather = inputFile.ReadLine() 
            key = CInt(strWeather.Substring(1, 3))
            'store the entire line in the collection
            colWeather.Add(strWeather, CStr(key))
        End While
    End If
End While

This way if your text file needs a revision in it's structure, as long as the new structure has the delimiter line your code will still work.

If .ShowDialog() = Windows.Forms.DialogResult.OK Then
    Try
        'Clears collection
        colWeather.Clear()
        'Opens the weather File
        inputFile = File.OpenText(strFileName)
        'Read each line after first seven lines
        For I = 1 To 7
            If inputFile.Peek >= 0 Then
                strWeather = inputFile.ReadLine()
                Stop
            End If
        Next
        While inputFile.Peek >= 0
            strWeather = inputFile.ReadLine()
            Stop
            key = CInt(strWeather.Substring(0, 3))
            Stop
            'store the entire line in the collection
            colWeather.Add(strWeather, CStr(key))
        End While
        lstAltitudes.Enabled = True
    Catch ex As Exception
        ' Show the exception's message.
        MessageBox.Show(ex.Message)

    End Try
End If
I only need the data below the first 7 lines you are correct, the first 8 lines are below

Does not seem like the blank line is the issue.  The for/next runs through the first 7 lines, and then on your example about line #9 even the first correct line of data is assigned to strWeather.  Now I get a conversion from string to integer is not valid when assigning the key.  I'm not sure how to store the 3 character string as the key (in this case "ABI").  Where did i go wrong in that code here. 


***data file***


    000 
    FBUS31 KWNO 101958
    FD1US1
    -DATA BASED ON 101800Z    
    VALID 110000Z   FOR USE 2000-0300Z. TEMPS NEG ABV 24000

    FT  3000    6000    9000   12000   18000   24000  30000  34000  39000
    ABI      1711+26 1709+17 1511+09 0506-04 0511-16 091130 091641 131353

Declare key as a string. The Collection Class expects the key to be a string.

In reference to my previous post, instead of If strWeather.Contains("**") Then you could try If strWeather.Split()(0) = "FT" Then, assuming FT is always consistent.

ah ha. Now on to my next issue! Thank you so much for the insight on that one. This stuff makes me cross-eyed

Glad you've got results. Please remember to mark this solved, and start a new thread for any new problems. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.