0

I'm using VBA and have come across this scenario where the variable is not displayed in the watch window when using interfaces:

enter image description here

Interface named ITest:

Public Property Get name() As String

End Property

Class named Class1:

Implements ITest

Private Property Get ITest_name() As String
    ITest_name = "T1"
End Property

Module named Module1 running test:

Public Sub main()
    Dim interface As ITest
    Set interface = New Class1
End Sub

Breakpoint at End Sub:

enter image description here

Calling the name member:

Calling the name member also seems to have no effect on the watch window

enter image description here

It would be great if I could use the watch window while using interfaces to debug code - has anyone else experienced this or know of a solution?

After altering the code: enter image description here

Many Thanks

Nick
  • 304
  • 1
  • 12
  • What happens if you include a call to your name method in the Sub main example.? At present you are just creating the instance, you don't actually call the methods on the instance. – freeflow Apr 15 '20 at 16:00
  • Thanks @freeflow unfortunately calling the name member also has no effect on the watch window, I've updated the original post with a screenshot – Nick Apr 15 '20 at 17:38

1 Answers1

0

I think the problem is occurring because there is no public reference to the instance of Class1 because the way in which you create the instance means that it is anonymous to VBA. If you Google you will find lots of recommendations to not use 'as new class' because of the issues it introduces.

The revised code below (which is more like how you would use an interface) shows the contents of myClass quite clearly

Revised Class1

Option Explicit

Implements ITest

Private Type Properties
    name As String
End Type

Private p As Properties

Private Property Get ITest_name() As String
    ITest_name = name
End Property

Private Property Let ITest_name(ByVal Value As String)
    name = Value
End Property

Public Property Let name(ByVal Value As String)
    p.name = Value
End Property

Public Property Get name() As String
    name = p.name
End Property

revised interface

Option Explicit

Public Property Get name() As String

End Property

Public Property Let name(ByVal Value As String)

End Property

Revised Module

Option Explicit

Public Sub main()

    Dim interface As ITest
    Dim myClass As Class1
    Set myClass = New Class1
    Set interface = myClass
    interface.name = "T1"
    Debug.Print interface.name

End Sub

If you intend working with interfaces (and good on you if you do) then you will find the interface generator in the VBA Rubberduck addin (under refactor) saves a lot of typing.

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • Thanks for the update to the code @freeflow Unfortunately although the name T1 is printed out in the immediate window it's still not showing in the watch window. I've updated the main post with a screenshot Not sure if it's perhaps a version specific bug? I'm using VBA 7.0 – Nick Apr 17 '20 at 09:46
  • Add a watch for the class instance (or better still try the locals window). The interface won't ever show you anything because it is intended only for compile time use to ensure method checking for objects passed to parameters specified as IType.. – freeflow Apr 17 '20 at 10:01
  • @freeflow, I am confused why I always see people putting class properties in a User Defined Type at the top. Is there any good reason for this as opposed to just having the variables outside the UDT at the top of the module? – Name Aug 05 '20 at 18:00
  • That.s a great question and one which would probably be best served by asking as a new question. In short the answer is intellisense, better organisation in the module, separation of concerns, for example I routinely use UDTs of Properties (p), State(s), Using (u) and Base (b). You can lump all of those together in a TSomething (This) if you wish. Thus if I have 10 property backing variables in the Properties UDT and a variable p as Properties then when I type p. VBAs Intellisense will give me a list of the UDT fields. THink what that means when revising that module 18 months later. – freeflow Aug 06 '20 at 08:45
  • Related [How to get property values of classes that implement an interface in the Locals window?](https://stackoverflow.com/questions/29146243/how-to-get-property-values-of-classes-that-implement-an-interface-in-the-locals) – Cristian Buse Sep 16 '20 at 10:03