[2005] adding objects to collection - slow?
I'm rewriting my app from a more classic Vb approach to a more OOP approach.
I got a class (in progress):
VB Code:
Public Class MyMovie
Private strMovie As String
Private strYear As String
Private SubExtensions() As String = {".srt", ".sub"}
Public Sub New(ByVal FullMovieName As String)
strMovie = FullMovieName
End Sub
Public ReadOnly Property Name() As String
Get
Return My.Computer.FileSystem.GetFileInfo(strMovie).Name.ToString
End Get
End Property
Public ReadOnly Property Folder() As String
Get
Return My.Computer.FileSystem.GetFileInfo(strMovie).Directory.FullName
End Get
End Property
Public Property Year() As String
Get
Return strYear
End Get
Set(ByVal value As String)
strYear = value
End Set
End Property
Public ReadOnly Property Subs() As Boolean
Get
Return HasSubs()
End Get
End Property
Private Function HasSubs() As Boolean
Dim strExt As String
Dim strMovieExt As String = strMovie.Trim.Substring((strMovie.Trim.Length) - 4, 4)
Dim strSub As String = strMovie.Trim.Substring(0, (strMovie.Trim.Length) - 4) 'strip extension
'is there a file with the same name and a sub extension ?
For Each strExt In SubExtensions
If System.IO.File.Exists(strSub & strExt) Then
Return True
Exit Function
End If
Next
Return False
End Function
End Class
Basicly I'm looping through a collection of files and depending on the extension Add an instance of my class to a Collection
VB Code:
Private colMovies As New Collection
VB Code:
(...)
If booIsMovie Then
If Not colMovies.Contains(strFile) Then 'Only add unique movies files to collection
Dim newMovie As New MyMovie(strFile)
colMovies.Add(newMovie, strFile)
End If
End If
(...)
Thing is, the adding to the collection is rather slow. Also when I loop through
the collection (for testing) and show a message box with some info on each movie object, there's a noticably delay between closing a messagebox and the next one showing up.
Is this normal or Am I doing something wrong ?
Re: [2005] adding objects to collection - slow?
Don't use Collection objects, which are hold-overs from VB6. Use classes from the System.Collections or System.Collection.Generic namespace.
Re: [2005] adding objects to collection - slow?
Quote:
Originally Posted by jmcilhinney
Don't use Collection objects, which are hold-overs from VB6. Use classes from the System.Collections or System.Collection.Generic namespace.
Ok, I'll look into those. any suggestions on what type is best used here ?
EDIT
I went with a generic Dictionary:
VB Code:
Private colMovies As Generic.Dictionary(Of String, MyMovie)
Performance wise this doesn't seem to make a difference?