【问题标题】:Object Required Error - Why would this be out of scope?需要对象错误 - 为什么会超出范围?
【发布时间】:2016-03-17 17:23:24
【问题描述】:

我想使用我自己的类(Markers 类)的全局对象数组,这些对象从记录集中加载数据。我可以将记录集中的数据加载到数组中的对象中,看起来很好,但是当我尝试访问数组中的一个对象中的值时,它会给出“需要对象”错误。我不明白为什么我的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 类对象时没有问题,但是在 sub 结束后它似乎被销毁了?我是 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

【问题讨论】:

  • 等一下,我认为问题可能出在我的类代码中...检查某些内容,如果是,将删除问题。
  • 我正要请你发布 Marker 类定义然后发现你的评论。
  • @Lankymart 刚刚编辑帖子以添加 TextLine 属性定义
  • 需要更多,最好是查看完整定义。
  • 其实这不是你为Property Get分配的TextLine吗?认为应该是TextLine = m_TxtLines(index)。你不应该在 TextLine 分配上需要 index

标签: arrays object vbscript scope


【解决方案1】:

在盯着类定义看了一会儿之后,我想我可能已经找到了罪魁祸首。

...中的任务

Public Property Get TextLine(index)

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

TextLine = m_TxtLine(index)

不是

TextLine(index) = m_TxtLine(index)

【讨论】:

  • 谢谢,就是这样,一点点校对可以大有帮助。
猜你喜欢
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 2015-09-12
  • 2017-12-02
  • 1970-01-01
相关资源
最近更新 更多