Rewriting app the OOP way - some questions
I picked up programming after a five years absence and just made my first app with VS2005. Though I did use as much as from the .dot framework as possibly could, the code is more classical VB in that I have a module and lots of functions and Subs.
Now I'm rewriting the app, the OOP way. So I'm struggling with classes and such :rolleyes:
My app basicly scans any folders the user selected for files of certain types.
If it is of the type I want, I want to 'Collect' it.
I made a class for the type i want, containing some properties and methods.
The one thing I'm not certain of is when I want to collect a file (ie add it),
should I add it to a collection of some sort within the class OR should I add an instance of the class for that file to an collection outside the class, ie een collection of objects?
Here's the code for the class (not finished yet):
VB Code:
Public Class MyMovie
Private strMovie As String
Private SubExtensions() As String = {".srt", ".sub"}
Private arrBadWords() As String = {}
Public Sub New(ByVal FullMovieName As String)
strMovie = FullMovieName
arrBadWords = BadWords()
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 Function IsMovie() As Boolean
'TO DO
End Function
Public 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
Public Function CleanName() As String
(...)
Return strMovie
End Function
Private Function BadWords() As Array
'Set the user defined "Bad Words" List
'When a custom list is saved it is loaded into table "BadWords" during startup (LoadSettings())
'If the list is empty populate with hardcoded list
Dim tbl As DataTable = frmMain.dsProgramSettings.Tables.Item("BadWords")
Dim row As DataRow
Dim item As String
Dim i As Short
Dim arrList() As String = {"divx", "xvid", "dvdrip", "dvdscr", "ws", "done", "dvd", "rip"}
If tbl.Rows.Count > 0 Then 'repopulate array list
ReDim arrList(tbl.Rows.Count - 1)
For i = 0 To UBound(arrList)
arrList(i) = tbl.Rows(i).Item("Word").ToString
Next
Else 'Populate dataset table
For Each item In arrList ' fill datatable
row = tbl.NewRow
row("Word") = item.ToString.ToLower
tbl.Rows.Add(row)
Next
End If
Return arrList
End Function
End Class
Re: Rewriting app the OOP way - some questions
A class would not contain a collection of itself. If you're talking about movies then you would define a Movie class and a MovieCollection class. The MovieCollection class would inherit CollectionBase. With strongly-typed collections you basically reproduce the functionality of the ArrayList but restrict the objects it can store to a certain type.