Link to home
Start Free TrialLog in
Avatar of Donoss
DonossFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Delphi assigning string filed to record type!

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  
Avatar of wildzero
wildzero

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.
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

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
Avatar of Donoss

ASKER

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
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.
Avatar of Donoss

ASKER

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
That is where you do
RecordType1.RecType := Edit1.Text;

I must be missing something :P


Avatar of Donoss

ASKER

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
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....
Avatar of Donoss

ASKER

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.....
ASKER CERTIFIED SOLUTION
Avatar of wildzero
wildzero

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial