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  
DonossAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wildzeroCommented:
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
bokistCommented:
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
bokistCommented:
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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

I must be missing something :P


0
DonossAuthor Commented:
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
wildzeroCommented:
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
DonossAuthor Commented:
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
wildzeroCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.