Solved

Simple: Help with arrays & sets problem

Posted on 2006-06-20
1
152 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
ID: 16947281
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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