Solved

Programming error

Posted on 2012-04-10
8
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 86

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 86

Expert Comment

by:Mike Tomlinson
ID: 37828500
What version VB are you using?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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