Solved

Delphi assigning string filed to record type!

Posted on 2007-03-30
11
181 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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
 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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