Solved

Simple: Help with arrays & sets problem

Posted on 2006-06-20
1
153 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 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

Technology Partners: 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

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…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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