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