8

I am developing a COM addin for Microsoft Excel, using VB.Net. I wrote a class to represent a worksheet that contains certain elements. Let's say for example it has a ListObject. I create a property for the ListObject like this:

Public Class MySheet

Private myTable as Excel.ListObject
Public Property Table() As Excel.ListObject
    Get
        Return myTable
    End Get
    Set(ByVal value As Excel.ListObject)
        myTable = value
    End Set
End Property

I then have properties (in MySheet class) that represent attributes of the ListObject, for example:

Private myTableStyle As String
Public Property TableStyle As String
    Get
        Return myTableStyle
    End Get
    Set(ByVal value As String)
        myTableStyle = value
        Me.Table.TableStyle = value
    End Set
End Property

The reason I have it set up this way is so that in my main code I don't have to update two properties every time I want to change the style (the TableStyle property of the MySheet class AND the TableSTyle property of the ListObject). So in my main code I can have:

Dim MySheetObject As MySheet = New MySheet()
MySheetObject.Table = SomeListObject
MySheetObject.TableStyle = "TableStyleMedium4"

That last line then stores both the string value as a property of the MySheet object and changes the TableStyle property of the ListObject in Excel.

Is this OK, or is it breaking some coding principle?

1

You may have other reasons to "cache" the TableStyle locally, but, otherwise, it may be seen as "premature optimisation". The property could be implemented as:

Public Property TableStyle As String
    Get
        Return Me.Table.TableStyle
    End Get
    Set(ByVal value As String)
        Me.Table.TableStyle = value
    End Set
End Property
1

Considering that this is a program that is inextricably linked to Excel, I think its relatively reasonable to treat an Excel.ListObject the same way you would normally treat a .Net object in this case.That being said, I don't think that it is worth the added complexity to have such a very small benefit as not needing two separate properties (but that has nothing to do with it being a COM object.)

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .