2015-09-25 45 views
-1

我有一個結構數組。 它是這樣宣稱的如何排序結構數組?

Public SongsList as New List(Of Song) 

「宋」它的結構的名稱。 它有2個變量:路徑和名稱; 我想知道如何按名稱排序這個數組。

Public Structure Song 
    Public Path as String 
    Public Name as String 
End Structure 

我想這

ListBox1.Items.Clear() 
Dim sorted = SongsList.OrderBy(Function(s) s.Name).ToList 
Dim i As Integer 
For i = 0 To sorted.Count - 1 
    ListBox1.Items.Add(sorted(i).Name.ToString) 
Next 

但它拋出一個NullReferenceException

這是我如何將項目添加到SongsList

Dim open As New OpenFileDialog 
open.Title = "Add songs" 
open.Filter = "MP3 Files(*.mp3)|*.mp3" 
open.Multiselect = True 
ListBox1.Items.Clear() 
If open.ShowDialog = DialogResult.OK Then 
    For Each SelectedSong As String In open.FileNames 
     i += 1 
     Dim songToAdd As New Song 
     songToAdd.Path = SelectedSong.ToString 
     songToAdd.Name = GetSafeFileName(SelectedSong.ToString) 
     SongsList.Add(songToAdd) 
     ListBox1.Items.Add(SongsList(i).Path) 
    Next 
End If 
+0

我編輯了你的q可以在OneFineDay的答案的評論中加入你提到的代碼;但請下一次嘗試包含您嘗試過的代碼 - 即使它不起作用。現在,你能告訴我們如何填寫'SongsList'嗎? –

+1

爲什麼你在已經是字符串的變量上調用'.ToString'? – Enigmativity

+0

重複的[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –

回答

0

可以使用Lambda表達式。它使用您在OrderBy函數中選擇的字段。讓我們覆蓋ToString方法來告訴列表框顯示什麼,然後你可以將列表設置爲數據源。

類:

Public Class Song 
    Public Property Path as String 
    Public Property Name as String 
    Public Overrides Function ToString() As String 
    Return Me.Path 
    End If 
End Class 

用法:

Dim open As New OpenFileDialog 
open.Title = "Add songs" 
open.Filter = "MP3 Files(*.mp3)|*.mp3" 
open.Multiselect = True 
If open.ShowDialog = DialogResult.OK Then 
    For Each SelectedSong As String In open.FileNames 
    Dim songToAdd As New Song 
    songToAdd.Path = SelectedSong 
    songToAdd.Name = GetSafeFileName(SelectedSong.ToString) 
    SongsList.Add(songToAdd) 
    Next 
End If 
Listbox1.DataSource = SongsList.OrderBy(Function(s) s.Name).ToList 
+0

我試過這個,但沒有奏效。 下面是代碼: ListBox1.Items.Clear() 昏暗排序= SongsList.OrderBy(功能(S)s.SongName).ToList 昏暗我作爲整數 對於i = 0到sorted.Count - 1個 ListBox1中.Items.Add(sorted(i).SongName.ToString) Next –

+0

NullReferenceException –

+0

您在上面寫道它是'Name'而不是'SongName'。哪一個? – OneFineDay

0

*非常類似OneFineDay的答案...

你並不需要一個自定義類,只需使用一個List(Of FileInfo)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim open As New OpenFileDialog 
    open.Title = "Add songs" 
    open.Filter = "MP3 Files(*.mp3)|*.mp3" 
    open.Multiselect = True 
    If open.ShowDialog = DialogResult.OK Then 
     ListBox1.DataSource = Nothing 
     Dim songs As New List(Of FileInfo) 
     For Each SelectedSong As String In open.FileNames 
      songs.Add(New FileInfo(SelectedSong)) 
     Next 
     songs = songs.OrderBy(Function(fi) fi.Name).ToList 
     ListBox1.DataSource = songs 
     ListBox1.DisplayMember = "Name" 
     ListBox1.ValueMember = "FullName" 
    End If 
End Sub 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    If ListBox1.SelectedIndex <> -1 Then 
     Dim fi As FileInfo = DirectCast(ListBox1.SelectedItem, FileInfo) 
     Dim name As String = fi.Name 
     Dim fullPath As String = fi.FullName 
     Debug.Print("name = " & name) 
     Debug.Print("fullPath = " & fullPath) 
    End If 
End Sub