DEEP copy of ArrayList containing RichTextBoxes ...

E

Esmail Bonakdarian

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
 
J

Jon Skeet [C# MVP]

Esmail Bonakdarian said:
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?
 
E

ebonak

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.


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?
 
M

MuZZy

Hello Jon,



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
 
E

ebonak

MuZZy said:
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
 
M

MuZZy

I think I understand the idea.

I do have a function that is associated with the "Closing" event and
executes when that happens.


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
 
E

Esmail Bonakdarian

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
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top