Arrays 对象必需错误-为什么会超出范围?
我想使用我自己的类(Arrays 对象必需错误-为什么会超出范围?,arrays,object,vbscript,scope,Arrays,Object,Vbscript,Scope,我想使用我自己的类(Markersclass)的对象的全局数组,这些对象具有从记录集中加载的数据。我可以将数据从记录集中加载到数组中的对象中,看起来很好,但是当我尝试访问数组中某个对象中的值时,会出现“需要对象”错误。我不明白为什么我的Markers()Marker类对象数组被破坏或超出范围 Dim Markers(6) Public Function GetItemSet(ByVal item) 'gets user input and returns a recordset objec
Markers
class)的对象的全局数组,这些对象具有从记录集中加载的数据。我可以将数据从记录集中加载到数组中的对象中,看起来很好,但是当我尝试访问数组中某个对象中的值时,会出现“需要对象”错误。我不明白为什么我的Markers()
Marker类对象数组被破坏或超出范围
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
在另一个脚本中,我尝试了直接使用标记(0).TextLine(0)
以及调用GetMarkerText(0,0)
来获取值。。。这两种方法都会在我直接尝试访问它的行上或在GetMarkerText
的一行代码中导致对象必需错误LoadMarkers
sub似乎在访问Markers()
类对象的Markers
数组时没有问题,但在该sub结束后它似乎会被销毁?我是VBScript新手,所以可能我不太了解范围是如何工作的,但我不明白为什么这不应该工作。有什么想法吗
编辑:我只是个上课的呆子吗?以下是标记
类定义的相关部分:
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
在盯着类定义看了一会儿之后,我想我可能已经发现了罪魁祸首 在…的作业
Public Property Get TextLine(index)
这是不对的。它应该指向
TextLine = m_TxtLine(index)
不是
等等,我想问题可能出在我的课程代码上。。。选中某个内容,将删除问题(如果是)。我正要请您发布
标记
类定义,然后发现了您的评论。@Lankymart刚刚编辑了帖子以添加文本行属性定义需要更多,理想情况下,您需要查看完整的定义。实际上,这不是您的属性获取文本行的赋值吗?我想应该是TextLine=m\u TxtLines(index)
。在文本行
作业中,你不需要索引
。谢谢你,就是这样,稍微校对一下就可以了。
TextLine(index) = m_TxtLine(index)