?
Solved

Simple: Help with arrays & sets problem

Posted on 2006-06-20
1
Medium Priority
?
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 320 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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