Reputation: 191
I want to use a global array of objects of my own class (Markers
class), which have data loaded in from a recordset. I can load the data from the recordset into the objects in the array just fine it seems, but when I try to access the values in the one of the objects in the array, it gives an "Object Required" error. I don't understand why my Markers()
array of Marker
class objects is getting destroyed or going out of scope.
Dim Markers(6)
Public Function GetItemSet(ByVal item)
'gets user input and returns a recordset object (just 1 record/row) from a SQL database
'working properly
End Function
Public Sub LoadMarkers(ByVal rs)
For i = 0 to 6
Set Markers(i) = New Marker
Next
MsgBox rs.Fields.Item("TextLine1").Value
Markers(0).TextLine(0) = rs.Fields.Item("TextLine1").Value
Markers(0).TextLine(1) = rs.Fields.Item("TextLine2").Value
'the above is just what I'm using to test functionality, no errors so far
End Sub
Public Function GetMarkerText(ByVal mrkr, ByVal line)
GetMarkerText = Markers(mrkr).TextLine(line)
End Function
In the other script I've tried both using Markers(0).TextLine(0)
directly as well as calling GetMarkerText(0,0)
to get the value... both methods result in object required error either on the line I directly try to access it or in the one line of code for GetMarkerText
. LoadMarkers
sub seems to have no issues accessing the Markers()
array of Marker
class objects, but then it seems to get destroyed after that sub ends? I'm new to VBScript so maybe I just don't quite understand how the scope is working but I can't see why this shouldn't work. Any ideas?
EDIT: Am I just a noob with Classes? Here's the relevant portion of the Markers
class definition:
Class Marker
Private m_Name
Private m_TxtLines(6)
Private m_ItemNum
Private m_FontSize
Private m_FontType
Private m_Length
Private Sub Class_Initialize( )
m_Name = "Unnamed"
m_ItemNum = 0
m_Length = 1
For i = 0 To 6
m_TxtLines(i) = ""
Next
m_FontSize = 8
m_FontType = "Arial"
End Sub
'Name Property
Public Property Get Name
Name = m_Name
End Property
Public Property Let Name(marker)
m_Name = marker
End Property
'TextLine Property for holding up to 7 lines of marker text
Public Property Get TextLine(index)
TextLine(index) = m_TxtLines(index)
End Property
Public Property Let TextLine(index, txt)
m_TxtLines(index) = txt
End Property
'ItemNum Property
Public Property Get ItemNum
ItemNum = m_ItemNum
End Property
Public Property Let ItemNum(num)
m_ItemNum = num
End Property
'Length Property
Public Property Get Length
Length = m_Length
End Property
Public Property Let Length(len)
m_Length = len
End Property
'FontSize Property
Public Property Get FontSize
FontSize = m_FontSize
End Property
Public Property Let FontSize(pts)
m_FontSize = pts
End Property
'FontType Property
Public Property Get FontType
FontType = m_FontType
End Property
Public Property Let FontType(font)
m_FontType = font
End Property
'Haven't added my methods in yet
End Class
Upvotes: 1
Views: 365
Reputation: 16671
After staring at the class definition for a while think I may have spotted the culprit.
The assignment in...
Public Property Get TextLine(index)
is not correct. It should just point to...
TextLine = m_TxtLine(index)
not
TextLine(index) = m_TxtLine(index)
Upvotes: 2