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

Can't Assign a TMemo to a TMemo?

426 views
Skip to first unread message

Wayne Herbert

unread,
Oct 16, 2003, 5:52:14 PM10/16/03
to
I've got a visible TMemo on a form. I want to save the TMemo to an
offscreen TMemo so I write

Memo2.Assign(Memo1);

When this line executes I get the error message, "Cannot assign a
TMemo to a TMemo"

WAZZUP? What do I need to do?

D5.1

Thanks.

Wayne Herbert

unread,
Oct 16, 2003, 6:06:31 PM10/16/03
to
This blows up because one of the memos (the offscreen memo) "has no
Parent Window".

Next up, I'll just use the text property. Thanks, John.

"John Herbster (TeamB)" wrote:

> Hi Wayne,
> If you just want to copy the contents, why not use
> Memo2.Lines.Assign(Memo1.Lines);
> {UNTESTED} Rgds, JohnH

John Herbster (TeamB)

unread,
Oct 16, 2003, 6:06:12 PM10/16/03
to

Guenther Wimpassinger

unread,
Oct 16, 2003, 6:17:16 PM10/16/03
to
A TMemo is a wrapper for a standard windows control. As long as you don't
have a parent, where this windows control can draw, it does not really exist,
so you can not assign the visible memo with an existing win-ctrl to an
invisible memo without the windows-handle.

I have not looked at the vcl-sources, but i think thats where the problem is.

just an idea
Guenther


Martin James

unread,
Oct 16, 2003, 6:51:35 PM10/16/03
to

"John Herbster (TeamB)" <herb...@sbcglobal.net> wrote in message
news:3f8f...@newsgroups.borland.com...

While that has been brought up. I've never understood 'assign'. The help
is unhelpful & confusing in this matter. To take the Tmemo strings, for
example. If I wanted to 'move' the strings content of a memo to another
memo, I might think that there should be an assignment method that makes
both Tmemos reference the same Tstrings, and that I could then clear the
'source' memo, leaving the 'destination' memo with the reference to the
strings. This would mean no copying.

This is, I understand, *not* what assign does. So what does 'assign' do
that is different from a straightforward copy?

Rgds,
Martin


Kevin

unread,
Oct 16, 2003, 6:54:51 PM10/16/03
to

Memo1.Text := Memo2.Text

Kevin


"Wayne Herbert" <wher...@NOSPAMkeymaps.com> wrote in message
news:3F8F130E...@NOSPAMkeymaps.com...

John Herbster (TeamB)

unread,
Oct 16, 2003, 8:40:16 PM10/16/03
to

"Martin James" <mjames...@dial.pipex.com> wrote
> ... I've never understood 'assign'. The help is unhelpful &
> confusing in this matter. ...

Martin, This difficulty, might be because there are so many
different Assign method actions -- all with the same name.

> So what does 'assign' do that is different from a
> straightforward copy?

I look at Assign as a copy, property by property,
from one object to another object. Rgds, JohnH

Peter Below (TeamB)

unread,
Oct 17, 2003, 5:34:06 AM10/17/03
to
In article <3F8F130E...@NOSPAMkeymaps.com>, Wayne Herbert wrote:
>
> I've got a visible TMemo on a form. I want to save the TMemo to an
> offscreen TMemo so I write

Which makes no sense in the first place. If you need a way to store the
content of the memo for later use, use either a simple string or a
Tstringlist:

aStringvar := memo1.text;

or

aStringlist.Assign( memo1.lines );

Don't use controls only as data containers. If the user does not
interact with them then using a control is a waste of memory and
windows resources.

> Memo2.Assign(Memo1);
>
> When this line executes I get the error message, "Cannot assign a
> TMemo to a TMemo"

You typically get that when the two components involved have been
created in different modules (EXE and DLL, for instance), which are not
compiled with run-time packages. In this case each module has its own
version of the TMemo class. They may have the same classname, but that
is not relevant in this context. The Assign method internally uses the
Is operator to check if the passed object is of the correct class. And
Is uses the address of the class record, not the class name, to
identify a class.
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


John Herbster (TeamB)

unread,
Oct 17, 2003, 8:00:41 AM10/17/03
to

"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote
> ... If you need a way to store the content of the memo
> for later use, use either a simple string or a Tstringlist ...

Thank you, Peter, for pointing out to the rest of us that
we ought to pause to think before putting our fingers
to work writing these responses. Regards, JohnH

Peter Below (TeamB)

unread,
Oct 17, 2003, 2:44:58 PM10/17/03
to
In article <3f8fd8a0$1...@newsgroups.borland.com>, John Herbster (TeamB)
wrote:

> Thank you, Peter, for pointing out to the rest of us that
> we ought to pause to think before putting our fingers
> to work writing these responses. Regards, JohnH

My pleasure <G>.

Martin James

unread,
Oct 18, 2003, 10:05:36 AM10/18/03
to
> I look at Assign as a copy, property by property,
> from one object to another object. Rgds, JohnH

OK, but, say with an object propery, like Tstrings, is the property
reference only copied, or the whole object? If I have a Tmemo with some
strings & assign them to another, empty, memo, how many Tstring objects are
there? One with two identical references, or two, with different
references?

I find this assign/copy stuff wholly confusing, especially after reading the
help, so I have never use 'assign', because I don't know what's going on.

I guess I should do some testing or look at the Delphi source for Tstrings
etc. to see what 'assign' really does, but I've never got around to it.

Rgds,
Martin


John Herbster (TeamB)

unread,
Oct 18, 2003, 10:30:12 AM10/18/03
to

"Martin James" <mjames...@dial.pipex.com> wrote

> > I look at Assign as a copy, property by property,
> > from one object to another object. Rgds, JohnH

> OK. But, say with an object propery, like Tstrings,
> is the property reference only copied, ...

Martin, If you code
Memo2.Lines := Memo1.Lines;
it will do a *copy* using the fLines.Assign() method.
That is because the property declaration looks like this
property Lines: TStrings read FLines write SetLines;
and the SetLines method calls the Assign method. But
notice that the "read" calls FLines, so that if you write
MyStrings := Memo1.Lines;
where is MyStrings is of type TStrings, then you can
use the reference (really just a typed pointer) MyStrings
to refer to the contents of Memo1.Lines, even as they
are changing.

> I guess I should do some testing or look at the
> Delphi source for Tstrings etc. to see what 'assign'
> really does, but I've never got around to it.

One easy way to see what is happening is to set the
option Project | Options | Compiler | Use Debug DCUs.
Then you can step with the debugger through the code
and watch what it is doing. Rgds, JohnH

Wayne Herbert

unread,
Oct 20, 2003, 12:06:34 PM10/20/03
to
Peter... the stringlist is what I was looking for... I wanted the Lines
property of the TMemo so that I can get the TextExtent of each line and
draw the lines back onto the screen in the form of a "for sale" sign. A
stringlist does what I need. thanks.
0 new messages