Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Access fields by number in a record

Posted on 2008-10-26
14
Medium Priority
?
280 Views
Last Modified: 2011-10-03
I need to be access the fields in a record sequentialy and to find the field type.

type
  STran = record
    TranType :array[0..1] of char;
    AccountRef :array[0..7] of char;
    NomCode :array[0..7] of char;
    DeptNo :integer;
    TransDate :array[0..9] of char;
    Net :currency;
    :
    :
  end;

example in Psudo Code
    for x:= 0 to STran.FIELDCOUNT -1 do
    begin
      case stTran.FIELDBYNUMBER[x].FIELDTYPE of
      String : //  process the value of thid field as a string;

      Currency ://
      Integer : //
      end; // end case

  Any suggestion on how to achiev this.

Thanks in advance
Alan
0
Comment
Question by:alanjbrown
[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
  • 7
  • 6
14 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 22808014
you can't.

but you could, if you would use a class instead of a record and make those record member to be published properties in the class. you will use RTTI for this (start from here: http://delphi.about.com/od/oopindelphi/a/delphirtti.htm )
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 22808542
Are you able to give me a more specific example of how RTTI can be applied to my question?
0
 
LVL 28

Expert Comment

by:2266180
ID: 22808584
are you willing to use a class instead of a record? I dont' want to waste my time if you do not ;)
0
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!

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 22808633
you could access them with a function or 2 you define
but for every new type of record ... you would have to create new functions

or use variant







type
  STranFieldCount = 10;
 
function STranField(Rec: STran; Index: Integer): Variant;
begin
  case Index of 
    0: Result := Rec.TranType;
    1: Result := Rec.AccountRef;
    2: Result := Rec.NomCode;
    3: Result := Rec.DeptNo;
    4: Result := Rec.TransDate;
    5: Result := Rec.Net;
  else 
    Result := Null;
  end;
end;
  
for x := 0 to STranFieldCount-1 do 
  case VarType(STranField(STran, x)) of 
    varString: Memo1.Lines.Add(VarToStr(STranField(STran, x)));
    varInt: Memo1.Lines.Add(VarToStr(STranField(STran, x)));
    ...
  end;

Open in new window

0
 
LVL 1

Author Comment

by:alanjbrown
ID: 22808758
Ciuly, I would like to try using a class instead of a record but I don't know anything about classes at the moment so I am not sure if it will create other problems for me.

Geert, I don't want to have to change my functions when ever the record structure is changed.
0
 
LVL 28

Expert Comment

by:2266180
ID: 22808771
well, try to give us examples of how you are using the record. if you are using a file of type record, then class won't do since you cannot have file of type class. there are other such scnearios, but better to see whatever you are using it for ;)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 22808794
I am using files of type record. The purpose of the program is to read the file and convert the output, the conversion being based on the field types in the record. So it looks as if I won't be able to use classes. I have potentially a lot of different structures to work with, that is why I need a generic process rather than to create different functions for each record structure.
0
 
LVL 28

Expert Comment

by:2266180
ID: 22808857
the let's go the other way around: what kind of processing do you need to do which is so generic that a function like geert suggests is too much? what are you trying to accomplish?
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 22808894
Output as CSV the first record to have the field names from the record, then when each field is output it will converted into a formatted string depending upon the actual field type. For example a currency field would be output as '99,999.00' etc.
0
 
LVL 28

Expert Comment

by:2266180
ID: 22808924
so you want to convert a binary file (with records) into a csv file?
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 22808948
yes but I want to add some proccessing / formatting to certain field types.
0
 
LVL 28

Accepted Solution

by:
2266180 earned 1000 total points
ID: 22811224
see this small demo I wrote: http://www.ciuly.com/delphi/rtti/binaryToCsv/index.html
read the explanations on the page on how to extend this to support most of the data types applicable.
if you have any questions about it, just ask.
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 22812157
Ciuly, thanks for the demo, it looks good although I won't be able to test it until this evening. I will get back to you later to award points etc.

I have an additional question regarding binary files which I will post as a new question.
0
 
LVL 28

Expert Comment

by:2266180
ID: 22812227
ok
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

705 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