Solved

Programming error

Posted on 2012-04-10
8
196 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

724 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