0 down vote favorite

I want to make my click procedure read five names on a list called names.txt file and store them in a five element one dimensional array and then arrage them in descending order what am i missing from this code that is keeping the file from displaying? nothing shows up in the display

this is what i tried

 Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
        Dim FineName As String = "names.txt"
        Dim names() As String


        If IO.File.Exists("names.txt") Then
            names = IO.File.ReadAllLines("names.txt")
            Array.Reverse(names)
        Else
            MessageBox.Show("The file   does not exist")
        End If
    End Sub
End Class

First check that names.txt contains what you think it does (it would help to post the file here).

I suggest you step through the code in the debugger and see if

  1. "names" is actually getting the data
  2. Array.Reverse(names) is doing what you want

Note that Array.Reverse does not do a reverse sort. It reverses the current order. To get a reverse sort you'll have to do

Array.Sort(names)        'sort names in ascending order
Array.Reverse(names)     'reverse the order

You'll have to be more careful with your code. For one thing, you probably meant to use the name "FileName" rather than "FineName". Also, there was not much point in creating that variable if you were just going to hard code the file name in

names = IO.File.ReadAllLines("names.txt")

However, the biggest thing keeping the names from being displayed is that you haven't written any code to display them. If you are using a ListBox then the easiest way is

ListBox1.Items.AddRange(names)

Dim FineName As String = "names.txt"

You must have to write the full path of the file "names.txt". If it resides at **D:\My Folder** then the codes is

Dim FineName As String = "D:\My Folder\names.txt"

unless, you would be unabled to open the file. Your codes should be

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

        Dim names() As String
        If IO.File.Exists("D:\My Folder\names.txt") Then
            names = IO.File.ReadAllLines("D:\My Folder\names.txt")

            'Write codes here to sort the array you desired
        Else
            MessageBox.Show("The file   does not exist")
        End If
    End Sub

Hope it can help you.

Yes by just reading through your code I noticed that you didn't specify where "names.txt" is located which is why you don't get nothing, if had use the Try block you would have noticed that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.