471,570 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

DEEP copy of ArrayList containing RichTextBoxes ...

Hello,

I am relatively new to C# but like it.

I have run into a problem to which I haven't been able to find
a solution despite looking.

I have an ArrayList that contains several RichTextBoxes (via Add())
I am trying to create a second ArrayList with this information,
but I want to make a DEEP copy instead of a shallow one, so that
this information is still around after the first ArrayList goes away.

I haven't been able to do this. I tried serializing/deserializing
the arraylist, but apparently RichTextBox isn't serializable.

Can anyone give me an idea how I can do a deep copy of this arraylist?
A concrete example would be even better ..

thanks!

Esmail
Dec 17 '05 #1
8 3046
Esmail Bonakdarian <eb******@t-hotmail.com> wrote:
I am relatively new to C# but like it.

I have run into a problem to which I haven't been able to find
a solution despite looking.

I have an ArrayList that contains several RichTextBoxes (via Add())
I am trying to create a second ArrayList with this information,
but I want to make a DEEP copy instead of a shallow one, so that
this information is still around after the first ArrayList goes away.
The information *would* still be around after the first ArrayList goes
away. The garbage collector won't collect the RichTextBoxes while you
still have references to them.
I haven't been able to do this. I tried serializing/deserializing
the arraylist, but apparently RichTextBox isn't serializable.

Can anyone give me an idea how I can do a deep copy of this arraylist?
A concrete example would be even better ..


It's not clear to me that you *really* need to do a deep copy. Are you
only worried about "losing" the RTB's, or do you want to be able to
change their contents independently?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 17 '05 #2
Hello Jon,
The information *would* still be around after the first ArrayList goes
away. The garbage collector won't collect the RichTextBoxes while you
still have references to them.
I haven't been able to do this. I tried serializing/deserializing
the arraylist, but apparently RichTextBox isn't serializable.

Can anyone give me an idea how I can do a deep copy of this arraylist?
A concrete example would be even better ..


It's not clear to me that you *really* need to do a deep copy. Are you
only worried about "losing" the RTB's, or do you want to be able to
change their contents independently?

This is my setup.

I have a class (multiEditor) that keeps track of some text in 5
RichTextBoxes which I keep in an ArrayList in that class.

The multiEditor class gets created by my main program for its
specific task, and then goes away.

What I would like to do is have access to the information in the
arraylist maintained temporarily by the multiEditor class. So I
wanted to create a function (property) in mulitEditor that would
return an arraylist to my main program containing the 5 RTBs.

The problem right now is that as the multiEditor class goes away
and the arraylist and RTBs are disposed, I also lose the
information.

I'm not quite sure about all of the semantics of C# .. so maybe
I'm missing something quite obvious here .... FWIW, I do not want
to modify the data in the ArrayList/RTBs in my main program,
simply have access to it so that I can display it in a
non-editable/modifyable (sp??) windows.

Hope that makes half-way sense explaining what I am trying to
do. If you have an idea on how to accomplish this I would be
grateful.

Thanks!

Esmail

ps: does the simple array object support deep copies? if so,
perhaps using arrays might be a better choice than
arraylists?

Dec 18 '05 #3
eb****@gmail.com wrote:
Hello Jon,
The information *would* still be around after the first ArrayList goes
away. The garbage collector won't collect the RichTextBoxes while you
still have references to them.
I haven't been able to do this. I tried serializing/deserializing
the arraylist, but apparently RichTextBox isn't serializable.

Can anyone give me an idea how I can do a deep copy of this arraylist?
A concrete example would be even better ..

It's not clear to me that you *really* need to do a deep copy. Are you
only worried about "losing" the RTB's, or do you want to be able to
change their contents independently?

This is my setup.

I have a class (multiEditor) that keeps track of some text in 5
RichTextBoxes which I keep in an ArrayList in that class.

The multiEditor class gets created by my main program for its
specific task, and then goes away.

What I would like to do is have access to the information in the
arraylist maintained temporarily by the multiEditor class. So I
wanted to create a function (property) in mulitEditor that would
return an arraylist to my main program containing the 5 RTBs.

The problem right now is that as the multiEditor class goes away
and the arraylist and RTBs are disposed, I also lose the
information.

I'm not quite sure about all of the semantics of C# .. so maybe
I'm missing something quite obvious here .... FWIW, I do not want
to modify the data in the ArrayList/RTBs in my main program,
simply have access to it so that I can display it in a
non-editable/modifyable (sp??) windows.

Hope that makes half-way sense explaining what I am trying to
do. If you have an idea on how to accomplish this I would be
grateful.

Thanks!

Esmail

ps: does the simple array object support deep copies? if so,
perhaps using arrays might be a better choice than
arraylists?


No, there is no such a thing as deep copy, the nearest is serialization.
And Array is no different form ArrayList in this case.
What's happening in your case is that RichText objects get destroyed
when their parent form(s) get closed.
So, what you need to do is to "unhook" those RTF's from their forms
before form is closed. You can do it by handling Closing event of those
forms.

Let me know if you need any further explanation.

Hope it helped,
MuZZy


Dec 18 '05 #4
MuZZy wrote:

No, there is no such a thing as deep copy, the nearest is serialization.
And Array is no different form ArrayList in this case.
What's happening in your case is that RichText objects get destroyed
when their parent form(s) get closed.
So, what you need to do is to "unhook" those RTF's from their forms
before form is closed. You can do it by handling Closing event of those
forms.
I think I understand the idea.

I do have a function that is associated with the "Closing" event and
executes when that happens.
Let me know if you need any further explanation.


How exactly do I unhook those RTFs? And if so, wouldn't I be left
with several individual RTFs then to return? Or am I unhooking
the arraylist with the RTFs from their form? How do I get that
info to my main program?

With regard to the last question, I suppose I could create a
property in the main class that could be set by my "Closing"
event handler in multiEdit passing the info to the main/parent
before going away.

If that makes sense, then the question remains on how to
unhook/disassociate the arraylist of RTFs from the multiEdit
class in the closing event handler?

Thanks for the help! I spent a considerable amount of time
trying various approaches w/o success before posting. I am
glad to have some leads & advice now.
Esmail

Dec 18 '05 #5
eb****@gmail.com wrote:
MuZZy wrote:
No, there is no such a thing as deep copy, the nearest is serialization.
And Array is no different form ArrayList in this case.
What's happening in your case is that RichText objects get destroyed
when their parent form(s) get closed.
So, what you need to do is to "unhook" those RTF's from their forms
before form is closed. You can do it by handling Closing event of those
forms.


I think I understand the idea.

I do have a function that is associated with the "Closing" event and
executes when that happens.
Let me know if you need any further explanation.


How exactly do I unhook those RTFs? And if so, wouldn't I be left
with several individual RTFs then to return? Or am I unhooking
the arraylist with the RTFs from their form? How do I get that
info to my main program?

With regard to the last question, I suppose I could create a
property in the main class that could be set by my "Closing"
event handler in multiEdit passing the info to the main/parent
before going away.

If that makes sense, then the question remains on how to
unhook/disassociate the arraylist of RTFs from the multiEdit
class in the closing event handler?

Thanks for the help! I spent a considerable amount of time
trying various approaches w/o success before posting. I am
glad to have some leads & advice now.
Esmail

Hi Esmail,

By "unhooking" an RTF from it's parent form i just mean:

RichBox1.Parent = null;

This way it will be removed from that form's Controls collection and
will not get disposed when the form is closed. So after that the RTF
will sort of "hang in the air" - you can put it to another form or do
whatever. When you no longer need it you can just remove it from the
array and it get collected by GC.

Keep in mind that you will need to set Parent to null for each RTF in
the array.

Let me know if you have more questions.

MuZZy
Dec 18 '05 #6
Hi again,
By "unhooking" an RTF from it's parent form i just mean:

RichBox1.Parent = null;

This way it will be removed from that form's Controls collection and
will not get disposed when the form is closed. So after that the RTF
will sort of "hang in the air" - you can put it to another form or do
whatever. When you no longer need it you can just remove it from the
array and it get collected by GC.
Very cool ... I didn't know about the Parent attribute/property. So
much to learn .. I guess you only learn by "doing" .. ;-)

So, each form knows its parent, makes sense.

By the way, somewhat related to this, is there a way for an object in C# to
know who invoked/created it? One way of course would be for the creator
to pass "this" into the object created, giving it a pointer to the
responsible class via the constructor. Just curious if C# provided
another mechanism.

Let me know if you have more questions.


Thanks, this has been very useful.

Actually, what I am doing now is copying the RTF text from each RichTextBox
and transferring it .. I think I'm doing this in a slightly more contorted way
than needed, but it may work ...

Esmail
Dec 20 '05 #7
Esmail Bonakdarian <eb*******@hotmail.com> wrote:

<snip>
By the way, somewhat related to this, is there a way for an object in C# to
know who invoked/created it? One way of course would be for the creator
to pass "this" into the object created, giving it a pointer to the
responsible class via the constructor. Just curious if C# provided
another mechanism.


Nope, that's the only way of doing it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 20 '05 #8
Jon Skeet [C# MVP] wrote:
Esmail Bonakdarian <eb*******@hotmail.com> wrote:
By the way, somewhat related to this, is there a way for an object in C# to
know who invoked/created it? One way of course would be for the creator
to pass "this" into the object created, giving it a pointer to the
responsible class via the constructor. Just curious if C# provided
another mechanism.

Nope, that's the only way of doing it.


Cool.. so I do know something ;-)

Thanks again!

Esmail
Dec 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by dan | last post: by
1 post views Thread by Marc Lefebvre | last post: by
4 posts views Thread by fperfect13 | last post: by
1 post views Thread by Marc Lefebvre | last post: by
6 posts views Thread by Desmond Cassidy | last post: by
2 posts views Thread by Lubomir | last post: by
22 posts views Thread by Steven Blair | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.