Solved

Simple: Help with arrays & sets problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

15 Experts available now in Live!

Get 1:1 Help Now