Solved

Programming error

Posted on 2012-04-10
8
191 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now