类函数中的错误 91

Posted

技术标签:

【中文标题】类函数中的错误 91【英文标题】:Error 91 in class function 【发布时间】:2015-04-10 18:32:35 【问题描述】:

使用 Access 2010,我正在收集信息并将其放到 Excel 电子表格中。当我运行下面的代码时,我得到了

运行时错误“91”:对象变量或未设置块

在我的课堂上这一行Set Cci = ChartColorItems(ColorID)Public Function GetRGB(ByRef ColorID As String) As Integer

“ChartColors”类:

Option Compare Database
Option Explicit

Private pChartColorItems As Collection

Public Property Get ChartColorItems() As Collection
  Set ChartColorItems = pChartColorItems
End Property
Public Property Set ChartColorItems(ByRef lChartColorItem As Collection)
  Set pChartColorItems = lChartColorItem
End Property

Public Function GetRGB(ByRef ColorID As String) As Integer
  Dim Cci As ChartColorItem
  Dim x As Integer

'---------------------------------------------------
'Error happens here:      
  Set Cci = ChartColorItems(ColorID)
'---------------------------------------------------
  x = RGB(Cci.Red, Cci.Green, Cci.Blue)
  GetRGB = x
  Set Cci = Nothing

End Function

Private Sub Class_Initialize()

  Dim Cci As ChartColorItem
  Dim Colors As Collection

  Set Colors = New Collection

  Set Cci = New ChartColorItem
  Cci.Red = 149
  Cci.Green = 55
  Cci.Blue = 53
  Cci.ColorID = "Pie1"
  Colors.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

  Set Cci = New ChartColorItem
  Cci.Red = 148
  Cci.Green = 138
  Cci.Blue = 84
  Cci.ColorID = "Pie2"
  Colors.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

End Sub

以及 ChartColorItem 类:

Option Compare Database
Option Explicit

Private pColorID As String
Private pRed As Integer
Private pGreen As Integer
Private pBlue As Integer

Public Property Get ColorID() As String
  ColorID = pColorID
End Property
Public Property Let ColorID(ByRef x As String)
  pColorID = x
End Property

Public Property Get Red() As Integer
  Red = pRed
End Property
Public Property Let Red(ByRef x As Integer)
  pRed = x
End Property

Public Property Get Green() As Integer
  Green = pGreen
End Property
Public Property Let Green(ByRef x As Integer)
  pGreen = x
End Property

Public Property Get Blue() As Integer
  Blue = pBlue
End Property
Public Property Let Blue(ByRef x As Integer)
  pBlue = x
End Property

当我调试时,代码通过ChartColorItems() getter 就好了,错误发生在End Function 之后,并把我放在上面提到的那一行。

这与我本周早些时候编写的一些代码非常相似,主要区别在于我使用 Class_Initialize 子填充我的 ChartColors,因为我试图存储一组固定的颜色,而我之前的代码正在收集数据并以更“正常”的方式将其插入到类中。

【问题讨论】:

不...如何/为什么看起来不正确?这正是我本周早些时候编写另一个函数的方式。 抱歉没有看到所有代码,我认为它的末尾是Set Cci = Nothing @StevenMartin 哪个?在Class_InitializeGetRGB 好吧,您先公开声明,然后私下声明,然后将其设置为空,因此当对象不存在时会出现 91 错误 【参考方案1】:

您在模块级别将私有集合定义为pCharColorItems,但您从未在类的初始化方法中对其进行初始化。相反,您使用本地范围的 Colors 集合变量。

Private Sub Class_Initialize()

  Dim Cci As ChartColorItem
  Dim Colors As Collection

  Set Colors = New Collection

您需要改用pChartColorItems

Private Sub Class_Initialize()

  Dim Cci As ChartColorItem
  Dim Colors As Collection

  Set pChartColorItems = New Collection

  Set Cci = New ChartColorItem
  Cci.Red = 149
  Cci.Green = 55
  Cci.Blue = 53
  Cci.ColorID = "Pie1"
  pChartColorItems.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

  Set Cci = New ChartColorItem
  Cci.Red = 148
  Cci.Green = 138
  Cci.Blue = 84
  Cci.ColorID = "Pie2"
  pChartColorItems.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

End Sub

但是GetRGB这一行还有一个bug。

 x = RGB(Cci.Red, Cci.Green, Cci.Blue)

RGB 函数返回一个长整数时,您将x 声明为整数。 “Pie1”的值会导致溢出错误。

【讨论】:

以上是关于类函数中的错误 91的主要内容,如果未能解决你的问题,请参考以下文章

去除drupal主题函数中的表单元素

保存电子邮件附件时出现运行时错误 '91

处理类构造函数中的承诺错误[重复]

构造函数中的类错误

挂载钩子中的错误:“TypeError:没有'new'就不能调用类构造函数节点”

Kotlin 数据类中的函数作为参数导致打包错误