Solved

Programming error

Posted on 2012-04-10
8
193 Views
Last Modified: 2012-04-10
I am trying to get a listbox in Visual Basic to output some text but I am getting string errors.  Any assistance offered on why this might be happening and how to fix it would be greatly appreciated.

Option Strict On

Public Class DLambertChp9_1dot

    ' class level private varibles
    Public Shared _intSizeOfArray As Integer = 21
    Public Shared _strEarthDayRunners(_intSizeOfArray) As String
    Private _intAge(_intSizeOfArray) As Integer
    Private _decDonationAmount(_intSizeOfArray) As Decimal
    Private Sub DLambertChp9_1dot_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim objReader As IO.StreamReader
        Dim intCount As Integer = 0
        Dim intfill As Integer


        'verify File exist
        If IO.File.Exists("earthdayrun.txt") = True Then
            objReader = IO.File.OpenText("earthdayrun.txt")


           

            'Intialize an instance of the Streamreader object and declare varibles
           
            Do While objReader.Peek <> -1
                _strEarthDayRunners(intCount) = objReader.ReadLine
                lstRunners.Items.Add(_strEarthDayRunners)
                _intAge(intCount) = CInt(CDec(objReader.ReadLine))
                _decDonationAmount(intCount) = CDec(objReader.ReadLine)
                intCount += 1

               
            Loop
            objReader.Close()
            For intfill = 0 To (_strEarthDayRunners.Length - 1)
                Array.Sort(_strEarthDayRunners)

                This is where I put data in the list box.
                lstRunners.Items.Add(_strEarthDayRunners)
            Next




        Else
            MsgBox("The file is not availible. Restart the program when the file is availible", , "Error")
            Me.Close()
        End If
    End Sub
End Class
0
Comment
Question by:regsamp
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37828336
Try something more like:
    Private Sub DLambertChp9_1dot_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objReader As IO.StreamReader
        Dim intCount As Integer = 0
        Dim intfill As Integer

        If IO.File.Exists("earthdayrun.txt") = True Then
            objReader = IO.File.OpenText("earthdayrun.txt")
            Do While objReader.Peek <> -1
                _strEarthDayRunners(intCount) = objReader.ReadLine
                _intAge(intCount) = CInt(CDec(objReader.ReadLine))
                _decDonationAmount(intCount) = CDec(objReader.ReadLine)
                intCount += 1
            Loop
            objReader.Close()

            Array.Sort(_strEarthDayRunners)
            lstRunners.Items.AddRange(_strEarthDayRunners)
        Else
            MsgBox("The file is not availible. Restart the program when the file is availible", , "Error")
            Me.Close()
        End If
    End Sub

Open in new window

0
 

Author Comment

by:regsamp
ID: 37828479
Thank you so much.  I will try that.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37828500
What version VB are you using?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:regsamp
ID: 37828708
VB 2008.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37828933
Try this:
Public Class DLambertChp9_1dot

    Public Class Runner
        Implements IComparable(Of Runner)

        Public Name As String
        Public Age As Integer
        Public DonationAmount As Decimal

        Public Overrides Function ToString() As String
            Return Me.Name
        End Function

        Public Function CompareTo(other As Runner) As Integer Implements System.IComparable(Of Runner).CompareTo
            Return Me.Name.CompareTo(other.Name)
        End Function

    End Class

    Private Runners As New List(Of Runner)

    Private Sub DLambertChp9_1dot_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
        Dim FileName As String = "earthdayrun.txt"
        If IO.File.Exists(FileName) = True Then
            Dim lines() As String = System.IO.File.ReadAllLines(FileName)
            If lines.Length Mod 3 = 0 Then
                For i As Integer = 0 To lines.Length - 1 Step 3
                    Dim rnnr As New Runner
                    rnnr.Name = lines(i)
                    rnnr.Age = CInt(lines(i + 1))
                    rnnr.DonationAmount = CDec(lines(i + 2))
                    Runners.Add(rnnr)
                Next

                Runners.Sort()
                lstRunners.DataSource = Nothing
                lstRunners.DataSource = Runners
            Else
                MessageBox.Show("Incorrect Number of Lines in the File!")
            End If
        Else
            MessageBox.Show("The file, '" & FileName & "', is not availible. Restart the program when the file is availible", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.Close()
        End If
    End Sub

    Private Sub lstRunners_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstRunners.SelectedIndexChanged
        Dim index As Integer = lstRunners.SelectedIndex
        If index <> -1 Then
            Dim rnnr As Runner = CType(lstRunners.Items(index), Runner)
            Debug.Print("Name: " & rnnr.Name)
            Debug.Print("Age: " & rnnr.Age)
            Debug.Print("Donation Amount: " & rnnr.DonationAmount)
        End If
    End Sub

End Class

Open in new window

0
 

Author Comment

by:regsamp
ID: 37829008
Okay, will try that one.  Thank you.
0
 

Author Comment

by:regsamp
ID: 37830457
That worked.  Thank you!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question