Solved

Simple: Help with arrays & sets problem

Posted on 2006-06-20
1
148 Views
Last Modified: 2010-04-05
Using:Delphi 7

Trying to come up with an efficient way to report and log errors. Here is a working solution/example if error codes are sequential.

//defined in a 3rd party dll header file:
TDisk         = $01
TTape        = $02
TPrinter      = $03
TProcessor = $04

//Code:
type
  TErrorType = (TDisk, TTape, TPrinter, TProcessor);

var
  TErrorTypeName: array[TErrorType] of
  string = ('Disk Drive', 'Tape Drive', 'Printer', 'Processor');

...
output := 'Error Received: ' + TErrorTypeName[TErrorType(TExampleResultRecord.ErrorResult)];


Problem is I'm importing from third party DLLs and error/status codes are not always sequential depending on what command I'm executing. What I tried and failed to do:

//defined in dll header file:
TDisk         = $E1
TTape        = $13
TPrinter      = $33
TProcessor = $24

//Code:
type
  TErrorType = (TDisk = $E1, TTape = $13, TPrinter = $33, TProcessor = #24); //set specific ordinal values to each element

var
  TErrorTypeName: array[TErrorType] of
  string = ('Disk Drive', 'Tape Drive', 'Printer', 'Processor');

...

***Compiler Error:
Number of elements differs from declaration

Just need some input on that most efficient/readable way of accomplishing this. Im trying to avoid creating a function to manually assign a name to a error/status code, since in reality I'm dealing with over 300 of 'em, thx.
0
Comment
Question by:Jebtrix
1 Comment
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 80 total points
Comment Utility
I don't think you can do it with 1 set, and I don't think you can do it without using at least 1 function.
However, itis not all as bad as you think
you can do this :-

output := 'Error Received: ' + TErrorTypeName[IndexOfErrorResult(TExampleResultRecord.ErrorResult)];

by having the following defined

const
    MaxErrorTypeElements = 4;
    TErrorTypeName: array[1..MaxErrorTypeElements] of string = ('Disk Drive', 'Tape Drive', 'Printer', 'Processor');
    TErrorTypeValue: array[1..MaxErrorTypeElements] of integer= ($E1, $13, $33, $24);

function TForm1.IndexOfErrorResult(errres: integer): integer;
    var
        i: integer;
    begin
        result := -1;
        i := 0;
        while (result = -1) and (i <= MaxErrorTypeElements) do
        begin
            if TErrorTypeValue[i] = errres then result := i else inc(i);
        end;
    end;

hth, Loki
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now