5 Replies - 1490 Views - Last Post: 02 October 2012 - 10:49 AM Rate Topic: -----

#1 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Displaying a list in Reverse order

Posted 01 October 2012 - 08:25 PM

Trying to get btnReverse to use a message box to show the list of dogs in last to first order. Everything else is working fine but I cant get the btnReverse to work. Any Suggestions?

Public Class Form1
    Private DogNames As New List(Of String)

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        Me.Close()
    End Sub

    Private Sub BtnAdmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdmit.Click
        Dim DogName As Integer
        DogNames.Add(txtDogName.Text)

        If txtDogName.Text = String.Empty Then
            MessageBox.Show("Please Enter a Name", "Entry Error")
        Else
            DogName = DogNames.Count
        End If
        txtDogName.SelectAll()
        txtDogName.Focus()
        LblShowDogs.Text = DogName
    End Sub

    Private Sub BtnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCheck.Click
        Dim Name As String = Trim(txtDogName.Text)

        If DogNames.Contains(Name) = False Then
            MessageBox.Show(Name & " is NOT in the kennel", "Non-Member")
        Else
            MessageBox.Show(Name & " is in the list", "Member")
        End If
    End Sub

    Private Sub BtnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDisplay.Click
        Dim Msg As String = String.Empty
        For Each entry In DogNames
            Msg &= entry & ", " & vbCrLf
        Next
        MessageBox.Show(Msg, "Current Members")
        txtDogName.Focus()
    End Sub

    Private Sub BtnReverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReverse.Click
        Dim DogTotal As Decimal = 0
        Dim dogs As Decimal
        Dim message As String = "" & vbCrLf

        For i As Integer = DogNames.Count - 1 To 0 Step -1
            dogs = DogNames(i) 'saying this InvalidCaseException was unhandled
            DogTotal += dogs
            message &= dogs & vbCrLf
        Next
        message &= " " &
        MessageBox.Show(message, "Current Members")

    End Sub

    Private Sub BtnEvict_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEvict.Click
        Dim name As String = Trim(txtDogName.Text)
        If DogNames.Remove(name) Then
            MessageBox.Show(name & " has been evicted from the kennel", "Evicted Member")
        Else
            MessageBox.Show(name & " is NOT in the list")
        End If
    End Sub

    Private Sub BtnEvictAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEvictAll.Click
        DogNames.Clear()
        LblShowDogs.Text = String.Empty
        txtDogName.Select()
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Displaying a list in Reverse order

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Displaying a list in Reverse order

Posted 01 October 2012 - 09:24 PM

DogNames is a string collection and your trying to set a decimal from a string and since it's a name it's not convertible by the compiler as a decimal.
Was This Post Helpful? 0
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Displaying a list in Reverse order

Posted 01 October 2012 - 10:36 PM

List(Of T) also has Reverse method. In your case you can just create new list on BtnReverse click, and add all DogNames to it, than .Reverse this new list, and display it.
Was This Post Helpful? 0
  • +
  • -

#4 JakeDay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-April 11

Re: Displaying a list in Reverse order

Posted 02 October 2012 - 09:33 AM

And how would I do that? I would look in my book but I am missing 50 pages lol and those 50 pages are the pages I need.
Was This Post Helpful? 0
  • +
  • -

#5 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 271
  • Joined: 22-August 11

Re: Displaying a list in Reverse order

Posted 02 October 2012 - 09:46 AM

Buy a new book, plus keep in mind that Google is your friend.
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 260
  • View blog
  • Posts: 1,419
  • Joined: 07-April 08

Re: Displaying a list in Reverse order

Posted 02 October 2012 - 10:49 AM

View Postlucky3, on 02 October 2012 - 12:36 AM, said:

List(Of T) also has Reverse method. In your case you can just create new list on BtnReverse click, and add all DogNames to it, than .Reverse this new list, and display it.


This would do what you want as far as making it easier to itterate.

Basically what you'll do is this:

'Start by creating a new List of strings 
Dim reverseDogList As new List(Of String)

'Now add the DogNames list to this list
reverseDogList.AddRange(DogNames)

'Reverse the order
reverseDogList.Reverse

'Itterate through the new list to get out the dog names
For Each dog as String in reverseDogList
'since it is a list of strings dog is of type string so do what you need to to create the comma seperated list of dog names
Next



This post has been edited by rgfirefly24: 02 October 2012 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1