Search code examples
vbaoopattributesrubberduck

How to set the Attribute VB_PredeclaredId using RubberDuck '@PredeclaredID


As a Rubberduck and OOP newbie I'm still learning how to use the software and take advantage of the programming techniques

First time I saw about folder annotations (https://github.com/rubberduck-vba/Rubberduck/wiki/Using-@Folder-Annotations) it was quite intuitive how it worked.

But then came the VB_Attribute Annotations (https://github.com/rubberduck-vba/Rubberduck/wiki/VB_Attribute-Annotations) which I didn't quite know how to made them work.

Hope this saves some time to peolple looking to use this feature

EDIT:

To contextualize the need of Attributes, here is a great post I found by Christopher J. McClellan

https://christopherjmcclellan.wordpress.com/2015/04/21/vb-attributes-what-are-they-and-why-should-we-use-them/

And the beauty about RubberDuck is that you don't need to do the Export/Edit/Import part manually, and it does it with a couple of clicks as described in the answer bellow.


Solution

  • To apply a predeclared id (set it to true) follow this steps:

    1- Enable the Rubberduck code explorer window

    enter image description here

    2- At the top of the class, add the text: '@PredeclaredID

    enter image description here

    Note: In the Code Explorer Window you can see when a Class has a Predeclared Attribute set to true because it has (PredeclaredId) in parenthesis next to its name

    enter image description here

    3- Launch Rubberduck "Code inspections"

    enter image description here

    4- Hit the "Refresh" button

    enter image description here

    5- In the "filter by descripcion" textbox type "pred"

    enter image description here

    6- Select the Issue and click the "fix" button

    enter image description here

    7- Select "Adjust attributes value"

    Now the code explorer should show the "Predeclared" tag next to the class name

    enter image description here