Author: Lou Adler
Better way to display [error] messages
Answer:
If you display more than a few [error] messages in your application, using a simple
method such as the following may not be the best approach:
1
2 Application.MessageBox('File not found', 'Error', mb_OK);
Above method of displaying errors will make it harder to modify actual messages
since they are distributed all over your application source code. It may be better
to have a "centralized" function that can display error messages, or better yet, a
centralized function that can display replaceable error messages. Consider the
following example:
3 type
4 cnMessageIDs =
5 (
6 nMsgID_NoError,
7 nMsgID_FileNotFound,
8 nMsgID_OutOfMemory,
9 nMsgID_ExitProgram
10 // list your other error
11 // IDs here...
12 );
13
14 const
15 csMessages_ShortVersion: array[Low(cnMessageIDs)..High(cnMessageIDs)] of PChar =
16 (
17 'No error',
18 'File not found',
19 'Out of memory',
20 'Exit program?'
21 // other error messages...
22 );
23
24 csMessages_DetailedVersion: array[Low(cnMessageIDs)..High(cnMessageIDs)]
25 of PChar =
26 (
27 'No error; please ignore!',
28
29 'File c:\config.sys not found.' +
30 'Contact your sys. admin.',
31
32 'Out of memory. You need ' +
33 'at least 4M for this function',
34
35 'Exit program? ' +
36 'Save your data first!'
37 // other error messages...
38 );
39
40 procedure MsgDisplay(
41 cnMessageID: cnMessageIDs);
42 begin
43 // set this to False to display
44 // short version of the messages
45 if (True) then
46 Application.MessageBox(csMessages_DetailedVersion[cnMessageID], 'Error', mb_OK)
47 else
48 Application.MessageBox(csMessages_ShortVersion[cnMessageID], 'Error', mb_OK);
49 end;
Now, whenever you want to display an error message, you can call the MsgDisplay()
function with the message ID rather than typing the message itself:
50
51 MsgDisplay(nMsgID_FileNotFound);
MsgDisplay() function will not only let you keep all your error messages in one place -- inside one unit for example, but it will also let you keep different sets of error messages -- novice/expert, debug/release, and even different sets for different languages.
|