Board index » delphi » Passing TMemoField as a TStringList parameter to a procedure

Passing TMemoField as a TStringList parameter to a procedure

I created a procedure that print the lines in a TStringList (or TStrings).
This procedure is declared as:

procedure PrintStringList(Items: TStringList);

How can I pass a TMemoField to this procedure.
I tried
  PrintStringList(MyMemoField)
  PrintStringList(MyMemoField as TStringList)
  PrintStringList(MyMemoField.AsVariant)
but all gave a compile time error.

Finally I build a 'wrapper' for a MemoField.

var
  MyStringList: TStringList
begin
  MyStringList := TStringList.Create;
  try
    MyStringList.Assign(MyMemoField);
    PrintStringList(MyStringList);
  finally
    MyStringList.Free;
  end;
end;

This worked out but means a lot of code for a simple action.
Is there a shorter way?

Thanks
R. Mittendorff
the Netherlands

 

Re:Passing TMemoField as a TStringList parameter to a procedure


Quote
On Fri, 29 Jan 1999 19:48:29 +0100, "R. Mittendorff" <r...@xs4all.nl> wrote:
>I created a procedure that print the lines in a TStringList (or TStrings).
>This procedure is declared as:

>procedure PrintStringList(Items: TStringList);

The TStringList object descends from TStrings and the TMemoField from
TField. That they are assignment-compatible is merely a factor of how the
Assign method of each is implemented. It is because of a lack of a closely
related ancestor that one cannot be passed as the other in a parameter.

One thing you might consider is typeing the parameter as an ancestor class
both have in common: TPersistent. With a parameter typed as TPersistent, an
object of either TMemoField or TStrings type may be passed. However, this
requires special handling within the function. Depending on what you re
doing in the function, you may need to use the Is operator to determine the
actual class of the passed object (TMemoField or TStrings) and then execute
different code based on that evaluation.

  procedure PrintStringList(Items: TPersistent);
  var
    SL: TStringList;
  begin
    SL := TStringList.Create;
    try
      if (Items is TMemoField) then
        SL.Assign(TMemoField(Items))
      else
        SL.Assign(TStringList(Items));
       {From here proceed with your normal printing code,
        except using SL instead of using Items directly.}
       ...
    finally
      SL.Free;
    end;
  end;

//////////////////////////////////////////////////////////////////////////
Steve Koterski                   "Writers have two main problems. One is
Technical Publications           writer's block, when the words won't come
INPRISE Corporation              at all, and the other is logorrhea, when
http://www.inprise.com/delphi    the words come so fast that they can
                                 hardly get to the wastebasket in time."
                                                    -- Cecelia Bartholomew

Re:Passing TMemoField as a TStringList parameter to a procedure


R.:

Try "PrintStringList(MyMemoField.Lines);"

Lines is a TStringList as well as the one you used in your workaround.
Thus, it should work that way.

Quote
> I created a procedure that print the lines in a TStringList (or
> TStrings).This procedure is declared as: procedure
> PrintStringList(Items: TStringList); How can I pass a TMemoField to
> this procedure.I tried  PrintStringList(MyMemoField)
> PrintStringList(MyMemoField as TStringList)
> PrintStringList(MyMemoField.AsVariant)but all gave a compile time
> error. Finally I build a 'wrapper' for a MemoField. var
> MyStringList: TStringListbegin  MyStringList := TStringList.Create;
> try    MyStringList.Assign(MyMemoField);
> PrintStringList(MyStringList);  finally    MyStringList.Free;
> end;end; This worked out but means a lot of code for a simple
> action.Is there a shorter way? ThanksR. Mittendorffthe Netherlands

--

Roland Salzburger
mailto:rol...@salzburger.de
http://www.salzburger.de
Fr?belstra?e 19, D-91058 Erlangen
Telefon +49-9131-12790-7, Fax -8

Re:Passing TMemoField as a TStringList parameter to a procedure


Roland,
which version of Delphi do you work with? In Delphi 4 the TMemoField has not
a property Lines.
Bye,
R. Mittendorff
the Netherlands

Roland Salzburger heeft geschreven in bericht
<36B2F33A.8F19A...@salzburger.de>...
R.:

Try "PrintStringList(MyMemoField.Lines);"

Lines is a TStringList as well as the one you used in your workaround.
Thus, it should work that way.

Quote
> I created a procedure that print the lines in a TStringList (or
> TStrings).This procedure is declared as: procedure
> PrintStringList(Items: TStringList); How can I pass a TMemoField to
> this procedure.I tried  PrintStringList(MyMemoField)
> PrintStringList(MyMemoField as TStringList)
> PrintStringList(MyMemoField.AsVariant)but all gave a compile time
> error. Finally I build a 'wrapper' for a MemoField. var
> MyStringList: TStringListbegin  MyStringList := TStringList.Create;
> try    MyStringList.Assign(MyMemoField);
> PrintStringList(MyStringList);  finally    MyStringList.Free;
> end;end; This worked out but means a lot of code for a simple
> action.Is there a shorter way? ThanksR. Mittendorffthe Netherlands

--

Roland Salzburger
mailto:rol...@salzburger.de
http://www.salzburger.de
Fr?belstra?e 19, D-91058 Erlangen
Telefon +49-9131-12790-7, Fax -8

Re:Passing TMemoField as a TStringList parameter to a procedure


Quote
>How can I pass a TMemoField to this procedure.
>I tried
>  PrintStringList(MyMemoField)
>  PrintStringList(MyMemoField as TStringList)
>  PrintStringList(MyMemoField.AsVariant)
>but all gave a compile time error.

Try
        PrintStringList(MyMemoField.AsString);

Michael Burton
AMS Services, Inc.
Coeur d'Alene, Idaho

Re:Passing TMemoField as a TStringList parameter to a procedure


Michael,
thanks for your reaction.

I tried it, but AsString returns a string type; this is not compatible with.
So it will also give a compile time error.
TStringList.

Bye,
R. Mittendorff
the Netherlands

Michael Burton heeft geschreven in bericht
<36b5c31a.1237...@iixnews.iix.com>...

Quote
>>How can I pass a TMemoField to this procedure.
>>I tried
>>  PrintStringList(MyMemoField)
>>  PrintStringList(MyMemoField as TStringList)
>>  PrintStringList(MyMemoField.AsVariant)
>>but all gave a compile time error.

>Try
> PrintStringList(MyMemoField.AsString);

>Michael Burton
>AMS Services, Inc.
>Coeur d'Alene, Idaho

Re:Passing TMemoField as a TStringList parameter to a procedure


It is not possible to assign TMemoField to TStringList. Further, there is no
TStrings or TStringList compatible property defined in TMemoField.
If PrintStringList wants a string list, why not give one to it?

List := TStringList.Create;
List.Text := MyMemoField.AsString;
PrintStringList(List);
List.Free;

Another tip:

function PrintStringList(AText: string): Boolean;
var
  List: TStringList;
begin
  Result := False;
  List := TStringList.Create;
  try
    List.Text := AText;
    {Do the printing stuff, like you do with PrintStringList now, or}
    Result := {ok or not}
  finally
    List.Free;
  end;
end;

Now it is easy:

if PrintStringList(MyMemoField.AsString) then...
if PrintStringList(MyOtherStringList.Text) then...
if PrintStringList(MyRichEdit.Lines.Text) then...
if PrintStringList(MyMemo.Lines.Text) then...

Hope that will help.
Christian Wallner

Quote
>>How can I pass a TMemoField to this procedure.
>>I tried
>>  PrintStringList(MyMemoField)
>>  PrintStringList(MyMemoField as TStringList)
>>  PrintStringList(MyMemoField.AsVariant)
>>but all gave a compile time error.

>Try
> PrintStringList(MyMemoField.AsString);

>Michael Burton
>AMS Services, Inc.
>Coeur d'Alene, Idaho

Other Threads