Solved

Delphi assigning string filed to record type!

Posted on 2007-03-30
11
179 Views
Last Modified: 2010-04-04
Hi,

Using D2006.  I want to define a file of records with different fixed formats e.g:

Rec1 = rectype char 3, field1 string 40, field2 string 30
Rec2 = rectype char 3, field1 string 25, field2 string 50, field3 string 10


I've defined these, however when I want to assign the data held in an edit field I get a compile error 'incompatible types' string and rec1.  How can I copy the contents of the editbox to the record?

Many thanks

Andy  
0
Comment
Question by:Donoss
  • 5
  • 4
  • 2
11 Comments
 
LVL 10

Expert Comment

by:wildzero
ID: 18827511
ur doing
rec1.field1 := edit1.text - not
rec1 := edti1.text right?

and your also doing
var
  Something : TRec1;
begin
so it'd be
  something.field1 := edit1.text;

it's late and im not thinking right, but it's something to check
can you also post the code for your whole type = record declaration.
0
 
LVL 6

Expert Comment

by:bokist
ID: 18828161
Hi !

Try this way:

type
  some_rec = record
           field_1   : array[1..3] of char;
           field_2   : string[40];
           field_3   : string[30];
  end;

var
   record_name : some_rec;
.....
  edit1.text := record_name.field_1;
  edit2.text := record_name.field_2;
  edit3.text := record_name.field_3;

regards,
  steve

0
 
LVL 6

Expert Comment

by:bokist
ID: 18828217
oops!

I forget another direction:

 record_name.field_1[1] := edit1.Text[1];
 record_name.field_1[2] := edit1.Text[2];
 record_name.field_1[3] := edit1.Text[3];
 record_name.field_2 := edit2.text;
 record_name.field_3 := edit3.text;

regards,
  steve
0
 

Author Comment

by:Donoss
ID: 18829504
Sorry but I didn't have the code on me, however here it is:

type
      TRec1 = record
      RecType: string[3];
      Field1: string[6];
      Field2: string[7];
      Field3: string[8];
    end;
    TRec2 = record
      RecType: string[3];
      Field1: string[8];
      Field2: string[13];
    end;
    TRec3 = record
      RecType: string[3];
      Field1: string[6];
      Field2: string[6];
      Field3: string[6];
      Field4: string[6];
    end;
  var RecordType1: TRec1;
      RecordType2: TRec2;
      RecordType3: TRec3;


procedure TForm6.Button1Click(Sender: TObject);
begin
  RecordType1 := Edit1.Text;
  Edit1.Text := RecordType1.RecType;
  Edit2.Text := RecordType1.Field1;
  Edit3.Text := RecordType1.Field2;
  Edit4.Text := RecordType1.Field3;
end;

The error is :

[Pascal Error] recordtypes.pas(65): E2010 Incompatible types: 'TForm6.TRec1' and 'TCaption'.

Essentially I want to be able to copy the contents of Edit1 to RecordTypes 1 to 3 depending on the record type and access the individual fields as applicable.

Many thanks

Andy
0
 
LVL 10

Expert Comment

by:wildzero
ID: 18829986
Just get rid of this line
  RecordType1 := Edit1.Text;

You are trying to assign a TEdit to your TRec1 which is where it falls over.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Donoss
ID: 18830283
Hi,

The point is I want to assign the value of Edit1.Text to RecordType1 so that I can then address the various record elements i.e field1 depending on the record type!

Andy
0
 
LVL 10

Expert Comment

by:wildzero
ID: 18831266
That is where you do
RecordType1.RecType := Edit1.Text;

I must be missing something :P


0
 

Author Comment

by:Donoss
ID: 18846093
Hi,

The value of edit1.text is say for instance RA 56789ASDFGHLKOOIUYTR.  RA is the record type and I eant to be able to test this value and then address the other record type elements.  I want to be able to assight recordtype1 the full value and then be able to address the individual field elements.  The code you have stated above will only assign RA to the edit1.text.

Am I making sense?  In COBOL this would be a redefines, i.e. one physical record can have many structures.

Many thanks

Andy
0
 
LVL 10

Expert Comment

by:wildzero
ID: 18846144
So....

using your code you could do this


procedure TForm6.Button1Click(Sender: TObject);
begin

  If Edit1.text = 'RA' then
{ or (which makes more sense) If Edit1.text = RecordType1.RecType then }
    begin
      Edit1.Text := RecordType1.RecType;
      Edit2.Text := RecordType1.Field1;
      Edit3.Text := RecordType1.Field2;
      Edit4.Text := RecordType1.Field3;
  end
else
  If () then
     begin
      Edit1.Text := RecordType2.RecType;
      Edit2.Text := RecordType2.Field1;
      Edit3.Text := RecordType2.Field2;
      Edit4.Text := ''
    end;
etc

Otherwise sorry I have no idea.
I need coffee I think....
0
 

Author Comment

by:Donoss
ID: 18852523
Maybe I'm missing something here but I can't assign the whole value RA 56789ASDFGHLKOOIUYTR to RecordType1.

I'm using an edit box to try and see how the code will work, but what I'm trying to do eventually is to read a text file of different types of record where record type 'RA' may have 3 different field elements field1, field2, field3 and another record type 'AP' may have 5 different fields etc......

So, I want to read the first record into a variable, then if the record type (first 3 char) is RA then I need to be able to manipulate the field elements for recordtype1, if it is AP then maniplulate different field elements as the record types have different structures.

Is that any clearer?

e.g:

RA FIELD1FIELD2FIELD3
AP FIELD1OFDATEFIELD2OFCHAR
DP FIELD1OFINTFIELD2OFCHARFIELS3OFCHARFIELD4OFCHAR

etc.....
0
 
LVL 10

Accepted Solution

by:
wildzero earned 500 total points
ID: 18853032
No because you are trying to do
RecordType1 := Edit1.Text;
Which is wrong as they are different types.

You could do this

If pos('RA', 'RA FIELD1FIELD2FIELD3') > 0 then
  begin
    //This is goingt to be a RecordType1
   
    //Fill the fields
   end
else
If pos('AP', 'AP FIELD1OFDATEFIELD2OFCHAR') > 0 then
  begin
    //this is going to be a RecordType2
   //Fill the fileds etc
etc
etc

   
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now