I have to implement a decision matrix in Delphi 7.
The function is
CalcNewStatus( actionCode: string; reportType: string; currentStatus: string): string;
- ActionCode can have the values ‘A’ and ‘N’
- ReportType can have the values ‘I’ and ‘F’
- CurrentStatus can have the values ‘P’, ‘I’, ‘F’.
In C# I would use a dictionary, for example. How can I do this in Delphi 7?
Normalize your input characters to zero-based ordinal values and things become much easier. Start with a few type declarations:
Then you can define an array using those types with all possible status values. Replace
sXwith whichever status value belongs in each spot.Then your function is simply this:
As you can see, most of this code is spent converting between strings and the enum types. If you can, avoid strings in this case. Switch to the enum types as early in your program as possible, and only convert back to strings or characters when you absolutely need to. Strings can have arbitrary length, which you really shouldn’t have to deal with, and strings can also have values outside the range you’ve defined. Enums can’t, unless you do something weird. Furthermore, the compiler won’t let you accidentally use a TState value where a TReportType is expected, which will help you from confusing your I’s and F’s.