是否有可能达到不属于接口的集合对象的属性

我有两类形状和一个接口。 在我从每个类实例化对象并设置它们的属性后,我将它们添加到一个集合中。 这非常简单。 然后我声明一个types为MyInterface的variables,然后遍历集合来添加每个形状。 但是,我想设置每种形状的附加属性,但不属于接口的一部分。 有没有办法做到这一点? 我认为其他语言称这种types的铸造,但我不知道。 VBA是否支持这个? 任何帮助表示赞赏。 我的代码如下:

界面iShape

 Option Explicit Public Property Let Top(value As Long) End Property Public Property Get Top() As Long End Property Public Property Let Left(value As Long) End Property Public Property Get Left() As Long End Property Public Property Let Width(value As Long) End Property Public Property Get Width() As Long End Property Public Property Let Height(value As Long) End Property Public Property Get Height() As Long End Property Public Function Draw(obj As Worksheet) As Excel.Shape End Function 

cDiamond

 Option Explicit Private pTop As Long Private pLeft As Long Private pWidth As Long Private pHeight As Long Private pColor As Long Implements iShape '====================Properties==================== Public Property Let Top(value As Long) pTop = value End Property Public Property Get Top() As Long Top = pTop End Property Public Property Let Left(value As Long) pLeft = value End Property Public Property Get Left() As Long Left = pLeft End Property Public Property Let Width(value As Long) pWidth = value End Property Public Property Get Width() As Long Width = pWidth End Property Public Property Let Height(value As Long) pHeight = value End Property Public Property Get Height() As Long Height = pHeight End Property Public Property Let Color(value As Long) pColor = value End Property Public Property Get Color() As Long Color = pColor End Property '====================Methods==================== Public Function Draw(obj As Worksheet) As Excel.Shape Set Draw = obj.Shapes.AddShape(msoShapeFlowchartOffpageConnector, Me.Left, Me.Top, Me.Width, Me.Height) End Function '====================Interface==================== Private Property Get iShape_Height() As Long iShape_Height = Height End Property Private Property Let iShape_Height(RHS As Long) Height = RHS End Property Private Property Get iShape_Left() As Long iShape_Left = Left End Property Private Property Let iShape_Left(RHS As Long) Left = RHS End Property Private Property Get iShape_Top() As Long iShape_Top = Top End Property Private Property Let iShape_Top(RHS As Long) Top = RHS End Property Private Property Get iShape_Width() As Long iShape_Width = Width End Property Private Property Let iShape_Width(RHS As Long) Width = RHS End Property Private Function iShape_Draw(obj As Worksheet) As Shape Set iShape_Draw = Draw(obj) End Function 

cTextbox

为简洁起见,除了具有Caption属性而不是Color属性之外,该类与cDiamond相同。

模块mTest

 Option Explicit Private Sub Test() Dim wks As Excel.Worksheet Set wks = ActiveSheet Dim c As Collection Set c = New Collection Dim d1 As cDiamond Set d1 = New cDiamond d1.Top = 10 d1.Left = 10 d1.Height = 25 d1.Width = 25 d1.Color = RGB(255, 0, 0) c.Add d1 Dim d2 As cDiamond Set d2 = New cDiamond d2.Top = 50 d2.Left = 10 d2.Height = 25 d2.Width = 25 d2.Color = RGB(0, 255, 0) c.Add d2 Dim t1 As cTextbox Set t1 = New cTextbox t1.Top = 90 t1.Left = 10 t1.Height = 25 t1.Width = 25 t1.Caption = "Textbox" c.Add t1 Dim shp As iShape For Each shp In c shp.Draw wks ' I would like to set the color or caption properties depending on the type of shape in the collection. Next shp Set c = Nothing End Sub 

如果我正确地理解了你(并且我也不完全理解接口),你应该能够通过声明types变体的shp来做你想要的。

 Dim shp For Each shp in C ... 

然后, shp将采用cDiamondcTextbox的types,具体取决于从Collection检索的types。 然后你将能够检索或修改shp.colorshp.caption 。 您可能还想要将Caption更改为cTextBox数据typesString

声明shp作为对象,你将失去intellisense。 另一种技术是让Interface返回对象实例的引用。 类似于OLEObject.Object如何返回它正在包装的对象的实例。 在我的例子中,我使用This来返回类的实例。

在这里输入图像说明

通过这种方式,您将拥有所有常见属性和方法的智能感知。 我们还可以使用With语句访问实现接口的类所特有的属性和方法。

你需要先testing一下对象的types。 然后使用With语句临时实例化一个新types的对象,并在With语句中写入代码。

 If TypeOf shp Is cTextbox Then With New cTextbox Msgbox .Caption End With End If 

最后,您只需将New实例replace为Object的实际实例即可。

 If TypeOf shp Is cTextbox Then With shp.This Msgbox .Caption End With End If 

在这里输入图像描述

界面(iShape)

 Public Function This() As Object End Function 

类cTextbox&cDiamond

 Public Function This() As Object Set This = Me End Function Public Function iShape_This() As Object Set iShape_This = This End Function 

mTest.Test

 Dim shp As iShape For Each shp In c shp.Draw wks If TypeOf shp Is cTextbox Then With shp.This MsgBox .Caption End With End If ' I would like to set the color or caption properties depending on the type of shape in the collection. Next shp