2015-09-25 68 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