Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert type (String to TField)

1,945 views
Skip to first unread message

Lygedas

unread,
Nov 22, 1999, 3:00:00 AM11/22/99
to
Hello,

I collide with one problem which I very need to solve.
In Table1 field A (type is string) I have value : "Table2.FieldByName("B")".
So I want :

var
Field1, Field3 : String;
Field2 : TField;

begin
Field1 : = Table1.FieldByName("A").Text;
Field2 : = (here I want to convert string type to TField type)
?????????????????????????
Field3 : = Field2.Value;
Print Field3; 'I must see Table2 field B value
end.

I will be very appreciate in your help.

Thanks.

Lygedas

Wayne Niddery (TeamB)

unread,
Nov 22, 1999, 3:00:00 AM11/22/99
to
Lygedas wrote in message <81bu4d$5n...@forums.borland.com>...

>
>I collide with one problem which I very need to solve.
>In Table1 field A (type is string) I have value :
"Table2.FieldByName("B")".
>So I want :
>
>var
> Field1, Field3 : String;
> Field2 : TField;
>
>begin
> Field1 : = Table1.FieldByName("A").Text;
> Field2 : = (here I want to convert string type to TField type)
>?????????????????????????
> Field3 : = Field2.Value;
> Print Field3; 'I must see Table2 field B value
>end.

You cannot "execute" a string as if it were compiled code. You will need to
parse this yourself. I would recommend storing it as "Table2.B" then it's
easier to use string routines to break this out:

var
Field1, Field3 : String;
tablename, fieldname: string;
table: TTable;
begin
Field1 : = Table1.FieldByName("A").Text; // gets 'Table2.B'
tablename := Copy(Field1, 1, Pos('.', Field1) - 1);
fieldname := Copy(Field1, Pos('.', Field1) + 1, 255);
// next line assumes you actually have a TTable
// component on the form called Table2
table := FindComponent(tablename);
Field3 := table.FieldByName(fieldname).AsString;
end;

--
Wayne Niddery - WinWright Consulting
RADBooks - http://members.home.net/wniddery/
Toronto Delphi Users Group - http://www.tdug.com/


Jaroslavas Akmanovas

unread,
Nov 23, 1999, 3:00:00 AM11/23/99
to
When I try to built, I receive message :
Incompatible types: 'TTable' and 'TComponents'


Thanks

Wayne Niddery (TeamB) wrote in message <81ccpa$8i...@forums.borland.com>...

Keith Johnson

unread,
Nov 23, 1999, 3:00:00 AM11/23/99
to

Jaroslavas Akmanovas <jaroslavas...@intelsoftas.lt> wrote in message
news:81dm82$go...@forums.borland.com...

> When I try to built, I receive message :
> Incompatible types: 'TTable' and 'TComponents'
> > table := FindComponent(tablename);

Just typecast->
table:=TTable(FindComponent(tablename));

Keith..

samueld...@gmail.com

unread,
May 12, 2014, 7:01:45 PM5/12/14
to
On Monday, November 22, 1999 12:00:00 AM UTC-8, Lygedas wrote:
> Hello,
>
> I collide with one problem which I very need to solve.
> In Table1 field A (type is string) I have value : "Table2.FieldByName("B")".
> So I want :
>
> var
> Field1, Field3 : String;
> Field2 : TField;
>
> begin
> Field1 : = Table1.FieldByName("A").Text;
> Field2 : = (here I want to convert string type to TField type)
> ?????????????????????????
> Field3 : = Field2.Value;
> Print Field3; 'I must see Table2 field B value
> end.
>
> I will be very appreciate in your help.
>
> Thanks.
>
> Lygedas

from Sam D. Hunt
You actually need three array structs to do what you are attempting.
using the following arrays... (see below arrays for continuation)

ARRAY 1:
FieldTypes: array [TFieldType] of String =
('ftUnknown', 'ftString', 'ftSmallint', 'ftInteger', 'ftWord', 'ftBoolean',
'ftFloat', 'ftCurrency', 'ftBCD', 'ftDate', 'ftTime', 'ftDateTime', 'ftBytes', 'ftVarBytes',
'ftAutoInc', 'ftBlob', 'ftMemo', 'ftGraphic', 'ftFmtMemo', 'ftParadoxOle', 'ftDBaseOle',
'ftTypedBinary', 'ftCursor', 'ftFixedChar', 'ftWideString', 'ftLargeint', 'ftADT', 'ftArray',
'ftReference', 'ftDataSet', 'ftOraBlob', 'ftOraClob', 'ftVariant', 'ftInterface', 'ftIDispatch',
'ftGuid', 'ftTimeStamp', 'ftFMTBcd', 'ftFixedWideChar', 'ftWideMemo', 'ftOraTimeStamp',
'ftOraInterval', 'ftLongWord', 'ftShortint', 'ftByte', 'ftExtended', 'ftConnection',
'ftParams', 'ftStream', 'ftTimeStampOffset', 'ftObject', 'ftSingle');

ARRAY 2:
sFieldTypes: array [1..52] of String =
('ftUnknown', 'ftString', 'ftSmallint', 'ftInteger', 'ftWord', 'ftBoolean',
'ftFloat', 'ftCurrency', 'ftBCD', 'ftDate', 'ftTime', 'ftDateTime', 'ftBytes', 'ftVarBytes',
'ftAutoInc', 'ftBlob', 'ftMemo', 'ftGraphic', 'ftFmtMemo', 'ftParadoxOle', 'ftDBaseOle',
'ftTypedBinary', 'ftCursor', 'ftFixedChar', 'ftWideString', 'ftLargeint', 'ftADT', 'ftArray',
'ftReference', 'ftDataSet', 'ftOraBlob', 'ftOraClob', 'ftVariant', 'ftInterface', 'ftIDispatch',
'ftGuid', 'ftTimeStamp', 'ftFMTBcd', 'ftFixedWideChar', 'ftWideMemo', 'ftOraTimeStamp',
'ftOraInterval', 'ftLongWord', 'ftShortint', 'ftByte', 'ftExtended', 'ftConnection',
'ftParams', 'ftStream', 'ftTimeStampOffset', 'ftObject', 'ftSingle');

ARRAY 3:
ftFieldTypes: array [1..52] of TFieldType =
(ftUnknown, ftString, ftSmallint, ftInteger, ftWord, ftBoolean,
ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime, ftBytes, ftVarBytes,
ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo, ftParadoxOle, ftDBaseOle,
ftTypedBinary, ftCursor, ftFixedChar, ftWideString, ftLargeint, ftADT, ftArray,
ftReference, ftDataSet, ftOraBlob, ftOraClob, ftVariant, ftInterface, ftIDispatch,
ftGuid, ftTimeStamp, ftFMTBcd, ftFixedWideChar, ftWideMemo, ftOraTimeStamp,
ftOraInterval, ftLongWord, ftShortint, ftByte, ftExtended, ftConnection,
ftParams, ftStream, ftTimeStampOffset, ftObject, ftSingle);

FIRST Step (using array 1):GET string name of the target TFieldType
sString := FieldTypes[MyTable.Fields[{field number}].DataType];

SECOND Step(using second array):
Step through sFieldTypes array until you find the value equal to sString in step 1.
Capture the sFieldTypes index in an integer, such as iIndex.

LAST Step (using array 3):
Your sString string value can be converted back to the original TFieldType thusly:

TFieldType := ftFieldTypes[iIndex];

This works great for me.
I use this strategy to get the field info from a table on a remote server over the internet and create a local copy in a TClientDataset. I then use the local client dataset to update the remote server using Indy 10 TCP client and server components. The only issue I haven't fully resolved is concurrency.

Good luck.
Sam Hunt

samueld...@gmail.com

unread,
May 14, 2014, 12:26:52 PM5/14/14
to
On Monday, November 22, 1999 12:00:00 AM UTC-8, Lygedas wrote:
> Hello,
>
> I collide with one problem which I very need to solve.
> In Table1 field A (type is string) I have value : "Table2.FieldByName("B")".
> So I want :
>
> var
> Field1, Field3 : String;
> Field2 : TField;
>
> begin
> Field1 : = Table1.FieldByName("A").Text;
> Field2 : = (here I want to convert string type to TField type)
> ?????????????????????????
> Field3 : = Field2.Value;
> Print Field3; 'I must see Table2 field B value
> end.
>
> I will be very appreciate in your help.
>
> Thanks.
>
> Lygedas

ok. so no one has read my post, yet. good. it had a couple of small errors.
array 2 and 3 should have been indexed 0..51, not 1..52. my strategy works either way, but 0..51 more properly describes the array indices.

Improvement...
I suggested searching for string found in step one in the second array.
Instead of...
sFieldTypes: array [1..52] of String =
('ftUnknown', 'ftString', 'ftSmallint', 'ftInteger', 'ftWord', 'ftBoolean',
'ftFloat', 'ftCu
...

I suggest using this array...
iFieldTypes: array [TFieldType] of String =
('0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '10', '11', '12', '13',
'14', '15', '16', '17', '18', '19', '20',
'21', '22', '23', '24', '25', '26', '27',
'28', '29', '30', '31', '32', '33', '34',
'35', '36', '37', '38', '39', '40',
'41', '42', '43', '44', '45', '46',
'47', '49', '50', '51');

then in step two, you don't need to step through the array to find a "ftString".
Instead of searching for "ftString", you will be converting "1".
In the array...
FieldTypes: array [TFieldType] of String =
('ftUnknown', 'ftString', 'ftSmallint', 'ftInteger', 'ftWord', 'ftBoolean',
'ftFloat', 'ftCurrency', 'ftBCD', 'ftDate', 'ftTime', 'ftDateTime', 'ftBytes', 'ftVarBytes',
'ftAutoInc', 'ftBlob', 'ftMemo', 'ftGraphic', 'ftFmtMemo', 'ftParadoxOle', 'ftDBaseOle',
'ftTypedBinary', 'ftCursor', 'ftFixedChar', 'ftWideString', 'ftLargeint', 'ftADT', 'ftArray',
'ftReference', 'ftDataSet', 'ftOraBlob', 'ftOraClob', 'ftVariant', 'ftInterface', 'ftIDispatch',
'ftGuid', 'ftTimeStamp', 'ftFMTBcd', 'ftFixedWideChar', 'ftWideMemo', 'ftOraTimeStamp',
'ftOraInterval', 'ftLongWord', 'ftShortint', 'ftByte', 'ftExtended', 'ftConnection',
'ftParams', 'ftStream', 'ftTimeStampOffset', 'ftObject', 'ftSingle');

the field type is then FieldTypes[strtoint('1')].
no stepping or looping.
This has to be faster and is definitely more elegant.
Sam Hunt

lionel...@gmail.com

unread,
Jun 10, 2017, 1:25:05 PM6/10/17
to
or a simplest way :

Field2.DataType := TFieldType(GetEnumValue(TypeInfo(TFieldType),Field2));

0 new messages