Constants in Code (Part 2)

In the previous post, we moved literal constant values into a separate module, together with ‘constructor’ functions for building values.  However, the scope of these constant and function declarations is the project: in other words a single template. So how can we share these declarations across multiple templates?

What we do is create a template (in our standard Templates directory), containing just the constant module(s): let’s call it Constants.dot. By default the template project will be called ‘Project’. We need to change this to some meaningful name, to avoid name clashes: in the Project Properties, let’s change this to ‘PConstants’.

Now comes the clever part: we can reference a template as a library, just as we reference the Word library. Within each template that uses the constants, go into >Tools >References…, browse to the Constants.dot file, and OK. Now whenever we open the client template it loads PConstants, with whatever declarations are currently in it.

Incidentally, you might notice that the PConstants project is unviewable: you can’t see its contents. You can also see that Constants.dot is loaded as a template via >Tools >Templates and Add-ins… in the Word application.

So now we have a single master copy of our constant declarations, which can be loaded by any client template that requires these. Any changes made to the master copy are propagated to the client templates.

Although we’ve described this in the context of Word, the same applies for the other applications. In the case of Excel, the constant module would live in an Add-in (.xla file), rather than a template.

Advertisements

1 Response to “Constants in Code (Part 2)”


  1. 1 geoffness February 7, 2009 at 10:52 am

    This is a very nice idea Roy – I can think of at least two applications in my current work. Thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




February 2009
M T W T F S S
« Jan   Mar »
 1
2345678
9101112131415
16171819202122
232425262728  

%d bloggers like this: