Solved

Programming error

Posted on 2012-04-10
8
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

919 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

16 Experts available now in Live!

Get 1:1 Help Now