473,582 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clone a refrenced type object?

Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmers use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated control at
runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn't have this kind
of method.
How can I create a clone of an object instance for controls like TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,

Aug 9 '06 #1
16 2498
Hamed,

If it is serializable you can serialize it and deserialize it.

http://www.vb-tips.com/dbPages.aspx?...c-61641f5c8d9d

I hope this helps,

Cor

"Hamed" <ha*******@yaho o.comschreef in bericht
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmers use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated control
at runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn't have this
kind
of method.
How can I create a clone of an object instance for controls like TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,

Aug 9 '06 #2
Hello

It seems that I should implement ICloneable to implement my own clone
object. the critical point for me is to make a control object based on
another control object that all of its event handlers are set like the old
one. Is there a way to do this job?

For example, is there a way to use EventInfo object to get all event
handlers of the old control in runtime and set my new cloned control events
to the event handlers of the old control?

Any suggestion is appreciated.

Regards
Hamed
Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmers use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated control
at runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn't have this
kind
of method.
How can I create a clone of an object instance for controls like TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,

Aug 12 '06 #3
The problem is that the EventInfo gives you info about the accessor,
bot the backing field - i.e. typically:

private EventHandler someEvent; // this is what we want
public event EventHandler SomeEvent { // this is what EventInfo reports
add {someEvent += value;}
remove {someEvent -= value;}
}

note: the compiler does this for you if you type:

public event EventHandler SomeEvent;

(noting that the name of the field can't be relied upon). So if you own
the Control and that specifc event, then you can do it easily as you
have visibility of the backing delegate field, but for external
Controls, or events in sub-classes, it is much harder. You would pretty
much need to walk the private fields.

Also: you should only really be cloning those event-handlers that you
own, since other code (than yours) my be subscribing that isn't
expecting that call (e.g. databinding implementations , etc).

My best advice normally is to simply re-apply the event handler s that
you know about, presumably in the same block of code that you use to
set the originals up in the first place (which probably means *not* the
designer-generated code). In your case, this may not be possible (due
to not being the setter-upper of the events); perhaps a bit of
misdirection would be in order? i.e. your code subscribes to the events
of all the controls, and their code subscribes to yours? And your code
manages the Clone operation, such that you can subscribee your code to
the Control's events... possibly... I think it could get very messy,
though...

Marc

Aug 12 '06 #4
Marc
Thanks for your comment. But the problem is:

I have a control that use in a DataGridColumnS tyle of my own. I want to give
the programmer the ability to assign its own event handlers. then there is
another column that needs a control exactly as previous (at least its events
are the same) and I need to clone the control. so I should at least copy the
properties and event handlers in the new control.

So I need to somehow copy them in the Clone method that is come from
ICloneable interface of my control's class.

Now how can I make a new control (suppose a TextBox from a class
MyOwnTextBox) that copies its properties and events?

Regards
Hamed

"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
The problem is that the EventInfo gives you info about the accessor,
bot the backing field - i.e. typically:

private EventHandler someEvent; // this is what we want
public event EventHandler SomeEvent { // this is what EventInfo reports
add {someEvent += value;}
remove {someEvent -= value;}
}

note: the compiler does this for you if you type:

public event EventHandler SomeEvent;

(noting that the name of the field can't be relied upon). So if you own
the Control and that specifc event, then you can do it easily as you
have visibility of the backing delegate field, but for external
Controls, or events in sub-classes, it is much harder. You would pretty
much need to walk the private fields.

Also: you should only really be cloning those event-handlers that you
own, since other code (than yours) my be subscribing that isn't
expecting that call (e.g. databinding implementations , etc).

My best advice normally is to simply re-apply the event handler s that
you know about, presumably in the same block of code that you use to
set the originals up in the first place (which probably means *not* the
designer-generated code). In your case, this may not be possible (due
to not being the setter-upper of the events); perhaps a bit of
misdirection would be in order? i.e. your code subscribes to the events
of all the controls, and their code subscribes to yours? And your code
manages the Clone operation, such that you can subscribee your code to
the Control's events... possibly... I think it could get very messy,
though...

Marc

Aug 12 '06 #5
Again - it depends if you own the events; this could be as simple as:

public MyTextBox Clone() {
MyTextBox tb = new MyTextBox(); // include anthing in the ctor that
is "readonly"
// fields and properties that we want to copy...
tb.PropertyA = this.PropertyA; // if accessible etc
tb.fieldB = this.fieldB; // declared in this type (not in a
base-class), or protected
tb.eventBackerC = this.eventBacke rC; // declared in this type (not in
a base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in
a base-class)
}

If you *don't* own them (i.e. declared in a base class, and no
protected access), then you need to either a: (preferably) rethink the
design, or b: use field-level reflection.

Let me know if I missed the point....

Marc

Aug 12 '06 #6
I don't understand the meaning of
tb.eventBackerC = this.eventBacke rC; // declared in this type (not in a
base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in a
base-class)
There is some event handlers assigned in other classes for example for Click
event. I want the cloned control to point to the event handler methods of
the old control. How to do this job?

Regards.
Hamed
"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Again - it depends if you own the events; this could be as simple as:

public MyTextBox Clone() {
MyTextBox tb = new MyTextBox(); // include anthing in the ctor that
is "readonly"
// fields and properties that we want to copy...
tb.PropertyA = this.PropertyA; // if accessible etc
tb.fieldB = this.fieldB; // declared in this type (not in a
base-class), or protected
tb.eventBackerC = this.eventBacke rC; // declared in this type (not in
a base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in
a base-class)
}

If you *don't* own them (i.e. declared in a base class, and no
protected access), then you need to either a: (preferably) rethink the
design, or b: use field-level reflection.

Let me know if I missed the point....

Marc

Aug 12 '06 #7
I think what you need here is probably not the clone() method, instead,
you probably want to declare your control as a base class, so other
people can inherit from them.

Then, you declare the event handler as protected methods, the new
control inherited from it can just simply override it.

Hamed wrote:
I don't understand the meaning of
> tb.eventBackerC = this.eventBacke rC; // declared in this type (not in a
base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in a
base-class)

There is some event handlers assigned in other classes for example for Click
event. I want the cloned control to point to the event handler methods of
the old control. How to do this job?

Regards.
Hamed
"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>Again - it depends if you own the events; this could be as simple as:

public MyTextBox Clone() {
MyTextBox tb = new MyTextBox(); // include anthing in the ctor that
is "readonly"
// fields and properties that we want to copy...
tb.PropertyA = this.PropertyA; // if accessible etc
tb.fieldB = this.fieldB; // declared in this type (not in a
base-class), or protected
tb.eventBackerC = this.eventBacke rC; // declared in this type (not in
a base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in
a base-class)
}

If you *don't* own them (i.e. declared in a base class, and no
protected access), then you need to either a: (preferably) rethink the
design, or b: use field-level reflection.

Let me know if I missed the point....

Marc

Aug 12 '06 #8
No. I have a several controls as fields in a container. In different
situations, some of them should be cloned. I just want a clone method that
accepts to clone the events too. :-(


"john sun" <js***********@ gmail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
>I think what you need here is probably not the clone() method, instead, you
probably want to declare your control as a base class, so other people can
inherit from them.

Then, you declare the event handler as protected methods, the new control
inherited from it can just simply override it.

Hamed wrote:
>I don't understand the meaning of
>> tb.eventBackerC = this.eventBacke rC; // declared in this type (not in a
base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in a
base-class)

There is some event handlers assigned in other classes for example for
Click event. I want the cloned control to point to the event handler
methods of the old control. How to do this job?

Regards.
Hamed
"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
>>Again - it depends if you own the events; this could be as simple as:

public MyTextBox Clone() {
MyTextBox tb = new MyTextBox(); // include anthing in the ctor that
is "readonly"
// fields and properties that we want to copy...
tb.PropertyA = this.PropertyA; // if accessible etc
tb.fieldB = this.fieldB; // declared in this type (not in a
base-class), or protected
tb.eventBackerC = this.eventBacke rC; // declared in this type (not in
a base-class)
tb.SimpleEventD = this.SimpleEven tD; // declared in this type (not in
a base-class)
}

If you *don't* own them (i.e. declared in a base class, and no
protected access), then you need to either a: (preferably) rethink the
design, or b: use field-level reflection.

Let me know if I missed the point....

Marc
Aug 12 '06 #9
Hamed,

Ultimately, you can't do it reliably.

If the method that is creating the clone is a member of the class being
cloned, then you can simply do:

// Inside clone method.
MyClass clone = new MyClass();
clone.MyEvent += this.MyEvent;

This assumes that you are declaring an event using the event keyword,
and not using add/remove handlers. It works because the C# compiler creates
a backing field of the same name (MyEvent).

However, if you declare your event like this:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}

Then the call to assign the event will fail, because the compiler will
not see the backing field that has the delegate in it, and revert to that.

Even if you are making the call from outside the type, the same problem
exists, since there is nothing on the metadata that links the event to the
backing field. There is nothing in the metadata that says that the backing
field has to be connected to the event. It's like asking for metadata on a
property, and then asking what the backing field is (or multiple backing
fields, since properties can really return anything they want, just like
event handlers).

So, that being said, there really isn't a reliable way to do this. The
best you could do is have an interface (if you plan on doing this across
multiple types) which would return a mapping of events to handlers, and then
when you clone the object, you would get those delegates and then perform
the clone.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Hamed" <ha*******@yaho o.comwrote in message
news:uF******** ******@TK2MSFTN GP03.phx.gbl...
Hello

It seems that I should implement ICloneable to implement my own clone
object. the critical point for me is to make a control object based on
another control object that all of its event handlers are set like the old
one. Is there a way to do this job?

For example, is there a way to use EventInfo object to get all event
handlers of the old control in runtime and set my new cloned control
events to the event handlers of the old control?

Any suggestion is appreciated.

Regards
Hamed
>Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmers use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated control
at runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn't have this
kind
of method.
How can I create a clone of an object instance for controls like TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,


Aug 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1568
by: Zürcher See | last post by:
I have a checkbox as sample, I have to create n-checkbox like that one in a webform, so I have extended the CheckBox class and wrote a public clone method that use the protected MemberwiseClone method. If I look the MemberwiseClone method definiton I find the following phrase: " Return Value A shallow copy of the current Object.
3
5298
by: scoobydoo | last post by:
Hello, I am trying to implement ICloneable's Clone() function, using Serialization. However, my code causes an exception. I have a class derived from TreeNode called "Node1". In Node1, I have implemented Clone() using serialization. When I call the Clone() function, the following exception is thrown at
4
6258
by: Steve Teeples | last post by:
I have TreeNodes in a TreeView, each contains unique data in its Tag section. I am trying to 'clone' a TreeNode and then modify the tag data of the cloned TreeNode. What I am seeing is that by modifying the Tag data of the cloned TreeNode the original TreeNode's Tag data is also being modified with the identical change. Does the clone...
4
4311
by: Brian Keating | last post by:
Hi there, Consider this from MSDN *Notes to Inheritors When you derive from DataGridViewCheckBoxCell and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to...
4
2526
by: csharpula csharp | last post by:
Hello, Have a basic question about Clone() If I am using the clone method on some object ,are the Cloned object properties going to change at every property change in original object? Thanks! *** Sent via Developersdex http://www.developersdex.com ***
2
11456
by: Steven | last post by:
Hi, I have created my own node (class MyNode : TreeNode) for a TreeView. To populate the treeview, i use something like MyNode newNode = new MyNode("Bla bla bla","0","1") for example. But, to move the nodes in the treeview, i use the clone() method... : MyNode theCopy = (MyNode)theTreeView.SelectedNode.Clone();
14
8599
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of...
3
46189
by: Rob Stevenson | last post by:
Does anyone know how to do this accurately. I really only want to clone the design-time properties which should make the task easier. I've searched high and low however and still can't find a problem-free solution. Even (ad-hoc) solutions posted by MSFT employees have problems. For instance, if you simply copy all serializable properties, you...
7
2822
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of...
0
7886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8183
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.