我有一個結構數組。 它是這樣宣稱的如何排序結構數組?
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
我編輯了你的q可以在OneFineDay的答案的評論中加入你提到的代碼;但請下一次嘗試包含您嘗試過的代碼 - 即使它不起作用。現在,你能告訴我們如何填寫'SongsList'嗎? –
爲什麼你在已經是字符串的變量上調用'.ToString'? – Enigmativity
重複的[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –