对象必需错误 - 为什么这超出范围?

时间:2015-12-11 17:52:19

标签: arrays object vbscript scope

我想使用我自己的类(Markers类)的全局对象数组,它们从记录集中加载数据。我可以将记录集中的数据加载到数组中的对象中,但是当我尝试访问数组中某个对象中的值时,会出现“Object Required”错误。我不明白为什么我的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

在另一个脚本中,我尝试直接使用Markers(0).TextLine(0)以及调用GetMarkerText(0,0)来获取值...这两种方法都会导致对象需要出错,我直接尝试访问它或GetMarkerText的一行代码。 LoadMarkers sub似乎没有问题访问Markers()Marker类对象,但是在子结束之后它似乎被破坏了?我是VBScript的新手,所以也许我只是不太明白范围是如何工作的,但我不明白为什么这不起作用。有什么想法吗?

编辑:我只是一个带课程的菜鸟吗?这是Markers类定义的相关部分:

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

1 个答案:

答案 0 :(得分:2)

在盯着课堂定义一段时间之后,我想可能已经发现了罪魁祸首。

......中的作业

Public Property Get TextLine(index)

不正确。它应该指向......

TextLine = m_TxtLine(index)

TextLine(index) = m_TxtLine(index)
相关问题