DesignBais Tip – Image Filenames in Code

Consider referring to images indirectly, rather than using filenames directly in code.

Here is a hard-coded example:
DBIMAGESPEC<1> = "LOGOFIELD"
DBIMAGESPEC<2> = "MyComp\logo1.jpg"

By referring indirectly to filenames you can change an image name throughout the application without searching for all occurrences of hard-coded filenames in code. For example:

* In an included code item
EQUATE E.COMPANY.LOGO.IMAGE TO "MyComp\logo1.jpg"

* In live code
DBIMAGESPEC<1> = "LOGOFIELD"
DBIMAGESPEC<2> = E.COMPANY.LOGO.IMAGE

Now, to change the company logo, just change the one item that has the equated reference and recompile all programs.

To avoid re-compilation, rather than actually EQUating the reference to the file, store it in a control table the same way most apps store the company name, address, etc, and read that when the app starts. Then carry it through Common. For example:

* In an included code item
READ COMPANY.INFO from F.CONTROL,"MASTER" THEN
  E.COMPANY.NAME = COMPANY.INFO<1>
  E.COMPANY.LOGO.IMAGE = COMPANY.INFO<16> ; * whatever atb…
END ELSE NULL ; * handle appropriately

Now any code that uses E.COMPANY.NAME or E.COMPANY.LOGO.IMAGE will get the current values and there is no need to hardcode anything or make a code change when such info changes. Of course this is the same thing we’d do with images of products, clients, etc – the data comes from the database rather than being hardcoded into the application. Use this coding technique consistently, regardless of the type of data.

These things might seem obvious to some developers but it when some people are writing GUI code they sometimes change techniques and neglect to take advantage of the well established tricks of the trade.

Leave a Reply