是否可以访问不属于接口的集合对象的属性

时间:2017-11-18 16:17:35

标签: excel vba excel-vba

我有两类形状和一个界面。在我从每个类实例化对象并设置其属性后,我将它们添加到集合中。这很简单。然后我声明一个类型MyInterface的变量并循环遍历集合以添加每个形状。但是我想要设置的每种类型的形状都有一个附加属性,但它不是界面的一部分。有没有办法做到这一点?我认为其他语言称这种类型为铸造,但我不确定。 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

为简洁起见,此类与 cDiamond 相同,不同之处在于它具有Caption属性而非Color属性。

模块 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

2 个答案:

答案 0 :(得分:1)

如果我理解正确,(我也不完全理解界面),你应该能够通过声明类型变体的shp来做你想要的。

Dim shp
For Each shp in C ... 
然后

shp将采用cDiamondcTextbox的类型,具体取决于从Collection检索到的内容。然后,您将能够检索或修改shp.colorshp.caption。您可能还想在Caption

中将String更改为数据类型cTextBox

答案 1 :(得分:1)

将shp声明为对象,您将失去智能感知。另一种技术是让Interface返回对象实例的引用。与OLEObject.Object如何返回它所包装的对象的实例类似。在我的示例中,我使用This来返回类的实例。

enter image description here

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

您需要先测试检查对象的类型。然后使用With语句临时实例化该类型的新Object,并在With语句中编写代码。

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

最后,您只需将New实例替换为Object的实际实例。

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

enter image description here

界面(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