Results 1 to 3 of 3

Thread: [2005] adding objects to collection - slow?

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    [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:
    1. Public Class MyMovie
    2.     Private strMovie As String
    3.     Private strYear As String
    4.     Private SubExtensions() As String = {".srt", ".sub"}
    5.  
    6.     Public Sub New(ByVal FullMovieName As String)
    7.         strMovie = FullMovieName
    8.     End Sub
    9.  
    10.     Public ReadOnly Property Name() As String
    11.         Get
    12.             Return My.Computer.FileSystem.GetFileInfo(strMovie).Name.ToString
    13.         End Get
    14.     End Property
    15.  
    16.     Public ReadOnly Property Folder() As String
    17.         Get
    18.             Return My.Computer.FileSystem.GetFileInfo(strMovie).Directory.FullName
    19.         End Get
    20.     End Property
    21.  
    22.     Public Property Year() As String
    23.         Get
    24.             Return strYear
    25.         End Get
    26.         Set(ByVal value As String)
    27.             strYear = value
    28.         End Set
    29.     End Property
    30.  
    31.     Public ReadOnly Property Subs() As Boolean
    32.         Get
    33.             Return HasSubs()
    34.         End Get
    35.     End Property
    36.  
    37.     Private Function HasSubs() As Boolean
    38.         Dim strExt As String
    39.         Dim strMovieExt As String = strMovie.Trim.Substring((strMovie.Trim.Length) - 4, 4)
    40.         Dim strSub As String = strMovie.Trim.Substring(0, (strMovie.Trim.Length) - 4) 'strip extension
    41.  
    42.         'is there a file with the same name and a sub extension ?
    43.         For Each strExt In SubExtensions
    44.             If System.IO.File.Exists(strSub & strExt) Then
    45.                 Return True
    46.                 Exit Function
    47.             End If
    48.         Next
    49.         Return False
    50.     End Function
    51.  
    52. 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:
    1. Private colMovies As New Collection

    VB Code:
    1. (...)
    2.                 If booIsMovie Then
    3.                     If Not colMovies.Contains(strFile) Then 'Only add unique movies files to collection
    4.                         Dim newMovie As New MyMovie(strFile)
    5.                         colMovies.Add(newMovie, strFile)
    6.                     End If
    7.                 End If
    8. (...)

    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 ?
    Using Visual Studio 2005 / Framework 2.0

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    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:
    1. Private colMovies As Generic.Dictionary(Of String, MyMovie)

    Performance wise this doesn't seem to make a difference?
    Last edited by SammyWaslow; May 1st, 2006 at 08:18 AM.
    Using Visual Studio 2005 / Framework 2.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width