Recent

Author Topic: TMemoField to StringList Assigment  (Read 3680 times)

plavicka

  • Newbie
  • Posts: 4
TMemoField to StringList Assigment
« on: April 07, 2014, 09:43:07 am »
Hi
I need to assign a value stored in an TMemoField to TStringList. My source code looks like this:

var
  SL: TStringList;
begin
   SL := TStringList.Create;
   SL.Assign(DB.FieldByName('SomeFieldName') as TMemoField);
end;

In Delphi this works fine, but in Lazarus an error message appears "Cannot assign a TMemoField to a StringList".

What is wrong?

Thank You

Pavel

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TMemoField to StringList Assigment
« Reply #1 on: April 07, 2014, 10:55:16 am »
SL.Assign(DB.FieldByName('SomeFieldName').AsString) ?

JZS

  • Full Member
  • ***
  • Posts: 194
Re: TMemoField to StringList Assigment
« Reply #2 on: April 07, 2014, 12:32:06 pm »
>   SL.Assign(DB.FieldByName('SomeFieldName') as TMemoField);
In which Delphi version you managed to compile that line?

I tried with Delphi XE, as well as Lazarus, no success.
Because the parameter that "SL.Assign" is expecting is TStrings, while TMemoField is TBlobField. So you will end up with "Invalid TypeCast" when you try that.

Playing around you end up getting "Class or Object types "TMemoField" and "TStringList" are not related"

Instead of TypeCasting you could have stored the value in a TStream variable and then loaded the stream to the StringList.

>   I need to assign a value stored in an TMemoField to TStringList.
But why complicating things, while the simplest text assigning can be (zeljko you meant this right?):
SL.Text := DB.FieldByName('SomeFieldName').ASString;

unless you plan to do something that is unclear to us, and hence you need to explain.
I use recent stable release

 

TinyPortal © 2005-2018