array of objects

  • Thread starter Thread starter Mike R.
  • Start date Start date
M

Mike R.

I have declared two classes. The first class has 4
private variables. Each has a property defined. I'm
calling a readfile sub from a second class. The second
class also has an Arraylist property.

I sub readfile and dim a new object (inside a loop) of
the first class and pass the four fields to each property
of the first class until the EOF. Within this loop I
want to add each object of the first class into the Array
list of the second class. I keep getting an Object
reference is not an instance of the object when adding to
the array.

I would appreciate any help.

Thanks

"this is piece of code when I load a form to read file
into array.
If OpenFileDialog1.FileName <> "" Then
Dim file1Obj As New clsMovieFile
(OpenFileDialog1.FileName)
file1Obj.ReadFile(file1Obj.FileName)



"First Class
Public Class clsMovie
Private mo_Title As String
Private mo_Year As Integer
Private mo_Category As String
Private mo_Director As String

Public Sub New(ByVal title As String, ByVal year As
Integer, ByVal category As String, ByVal director As
String)
mo_Title = title
mo_Year = year
mo_Category = category
mo_Director = director

End Sub
Public Property MovieTitle() As String
Get
Return mo_Title
End Get
Set(ByVal Value As String)
mo_Title = Value
End Set
End Property

Public Property MovieYear() As Integer
Get
Return mo_Year
End Get
Set(ByVal Value As Integer)
mo_Year = Value
End Set
End Property
Public Property MovieCategory() As String
Get
Return mo_Category
End Get
Set(ByVal Value As String)
mo_Category = Value
End Set
End Property
Public Property MovieDirector() As String
Get
Return mo_Director
End Get
Set(ByVal Value As String)
mo_Director = Value
End Set
End Property
Public Function FormatLine(ByVal formatrec As String)
As String
Dim listMovie As String
listMovie = formatrec.Substring(0, 69) & "|" & _
formatrec.Substring(73, 11) & "|" & _
formatrec.Substring(69, 4) & _
" " & _
formatrec.Substring(84, 25)
Return listMovie
End Function
End Class


"Second Class - below is readfile sub
Public Class clsMovieFile
Private mo_FileName As String
Private mo_RecordCount As Short
Private mo_MovieArr As ArrayList

Public Sub New(ByVal filename As String)
mo_FileName = filename
End Sub
Public Property FileName() As String
Get
Return mo_FileName
End Get
Set(ByVal Value As String)
mo_FileName = Value
End Set
End Property
Public Property RecordCount() As Short
Get
Return mo_RecordCount
End Get
Set(ByVal Value As Short)
mo_RecordCount = Value
End Set
End Property
Public Property MovieArr() As ArrayList
Get
Return mo_MovieArr
End Get
Set(ByVal Value As ArrayList)
mo_MovieArr = Value
End Set
End Property

Public Sub ReadFile(ByVal file As String)

'Try 'Open file and trap any errors.
FileOpen(1, file, OpenMode.Input)
recCount = 0
Dim file2Obj As New clsMovieFile(FileName)
Do Until EOF(1) 'read until end of file and load
list box

Dim movObj As New clsMovie(LineInput
(1).Substring(0, 69), _
LineInput(1).Substring
(69, 4), _
LineInput(1).Substring
(73, 11), _
LineInput(1).Substring
(84, 25))


recCount = recCount + 1
file2Obj.MovieArr.Add(movObj) """This is
where I get an error.

Loop

FileClose(1)

'Catch
MsgBox("Error Opening File: " &
Err.Description, , "Movie Search")
'Finally
FileClose(1)
'End Try

End Sub
End Class
 
Back
Top