|
ok, this works well, thanx alot
|
|
|
|
|
Is it possible to make an exact copy of an instance of an object (not its reference)?
This is what I'd like to achieve:
1 make a copy of an instance at a certain moment to save the state it's in
2 modify the original for a bit
3 replace the original object with the saved one later on
This way I'd like to be able to "recall" the state my object was in at the moment I made a copy. Is this possible without manually constructing a new object of this type and copying all of it's members?
|
|
|
|
|
To do this, you can binary serialize the object. Serialize the object to a memory stream and then deserialize to a new instance. For classes that don't have a CopyTo method, this is pretty helpful. However, the class that you're serializing has to have the Serializable attribute.
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
Actually it is Serializable since it can be stored to disk too. I just had a go with this but I haven't serialized to memory before and for some reason I'm getting a
System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.<br />
The code looks like this.
Collection<MyOutput> myOutputCollection = new Collection<MyOutput>();
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
OutputData myOutput;
bFormatter.Serialize(mStream, myOutput);
deserializedOutput = (OutputData)bFormatter.Deserialize(mStream);
myOutputCollection.add(deserializedOutput);
mStream.Close();
I'm serializing and deserializing the object, then adding it to a collection, since I'm still using it from the collection once it's added (it's not like I don't need it anymore).
|
|
|
|
|
Before you deserialize, you have to rewind the memory stream back to the initial position.
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
This works, thx
|
|
|
|
|
In short you want something to that gives you an 'undo' feature. I don't like recommending 'design patterns' without looking at several alternatives, but in this case the Memento pattern fits well.
Googling will give you a solid explanation.
Wikipedia has a short code sample:
Memento pattern[^]
|
|
|
|
|
I see the use of the pattern, but it seems this will only work for a single member that defines the state, while I meant the state of the object itself and all of its members... I'm afraid this won't work, but thanks for the info.
|
|
|
|
|
You could do the following:
Make the following changes to your class:
public class MyClass : ICloneable
{
public MyClass(){}
public MyClass(object newInstance)
{
if ( !(newInstance is MyClass) ) throw new Exception...
MyClass instance = (MyClass)newInstance;
}
public object Clone
{
return (object)this;
{
}
Then in your code you would do as follows:
...
MyClass newObject = new MyClass(oldObject.Clone());
Unfortunately, there is no ICloneable<t> and object is all you can pass.
|
|
|
|
|
Wesner Moise has a good blog posting[^] regarding how to clone objects that don't implement ICloneable. He basically uses reflection to invoke the protected MemberwiseClone method.
If you have the source code to this object, I'd just make it implement ICloneable.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Messianic Instrumentals (with audio)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Looks interesting, I'll try this out...
|
|
|
|
|
Works perfectly... Thank you.
|
|
|
|
|
I'm doing this using the Clone method of the object. I think there's no universal mechanism for copying objects, so you need to implement this method by your own. If you are working with more types, it is good to use some ICloneable interface.
This is an example:
<br />
public interface ICloneable {<br />
<br />
object Clone();<br />
}<br />
<br />
public class BitmapWithArray : ICloneable {<br />
<br />
public Bitmap bitmap = null;<br />
public int[] array = null;<br />
public ushort someValue = 0;<br />
<br />
public object Clone() {<br />
<br />
BitmapWithArray obj = new BitmapWithArray();<br />
<br />
if (this.bitmap != null)<br />
obj.bitmap = (Bitmap)this.bitmap.Clone();<br />
<br />
if (this.array != null)<br />
obj.array = (int[])this.array.Clone();<br />
<br />
obj.someValue = this.someValue;
}<br />
}<br />
|
|
|
|
|
Why does the cursor moves one tab ahead when i press Ctrl + I key combination?
Thanks,
Pramod
|
|
|
|
|
Uhhmm because you pressed the Ctrl+I key?
If you want a different behaviour, you must program it.
private void richTextBox1_KeyPress( object sender, KeyPressEventArgs e )
{
if ( e.KeyChar == Keys.ControlKey & Keys.I )
{
richTextBox1.Font = new Font( "Arial", FontStyle.Italic );
e.Handled = true;
}
}
|
|
|
|
|
I want to change the Font Style to Italics,
and i have written the following code:
if (e.KeyCode == Keys.B && e.Modifiers == Keys.Control)
{
tbbTextFormatting_ButtonClick(tbbTextFormatting, new ToolBarButtonClickEventArgs(tbbBold));
}
else if (e.KeyCode == Keys.I && e.Modifiers == Keys.Control)
{
tbbTextFormatting_ButtonClick(tbbTextFormatting, new ToolBarButtonClickEventArgs(tbbItalic));
}
else if (e.KeyCode == Keys.U && e.Modifiers == Keys.Control)
tbbTextFormatting_ButtonClick(tbbTextFormatting, new ToolBarButtonClickEventArgs(tbbUnderline));
is something wrong in this code?
the Bold and Underline work fine
but for italics the cursor moves one tab ahead.
Thanks & Regards,
Pramod
|
|
|
|
|
Try this...
if (e.KeyCode == Keys.B && e.Modifiers & Keys.Control != 0)
{
Reformat(tbbTextFormatting, new ToolBarButtonClickEventArgs(tbbBold));
}
else if (e.KeyCode == Keys.I && e.Modifiers & Keys.Control != 0)
{
Reformat(tbbTextFormatting, new ToolBarButtonClickEventArgs(tbbItalic));
}
else if (e.KeyCode == Keys.U && e.Modifiers & Keys.Control != 0)
Reformat(tbbTextFormatting, new ToolBarButtonClickEventArgs(tbbUnderline));
|
|
|
|
|
Still the cursor moves one tab ahead from the current location.
Pramod
|
|
|
|
|
Are you setting e.Handled = true ???
|
|
|
|
|
NO,
When should i set that?
after my code or before that.
Thanks & Regards,
Pramod
|
|
|
|
|
Hi guys,
I need some help, I am working on validation of the product and I was wondering if you guys could give me some suggestions on how to do it. Basically what I intend to do is to have 1 month, 6 months and 12 month product keys. I am having trouble especially figuring out how to monitor the time; what if the user changes time on their system? etc.
Please help
|
|
|
|
|
At this moment there is no hack free easy solution for this problem.
|
|
|
|
|
you can try one thing,
Store the Key, The Registratrion Date and the period in the database
and whenever u need to check the validity use the database servers current date instead of the local machine date.
I guess u dont change the database servers date.
Hope that works for u.
Regards,
Pramod
|
|
|
|
|
This response might work for the one who asked , but on a theoretical level their is a problem with this solutions. The problem is that you need the client to connect to the server all the time (or by the start of the application).
1 The client can block your app in the firewall.
2 And what if the app is installed on a labtop? One cannot block out users that don't have a connection to the internet 24/7.
|
|
|
|
|
You have a point,
my solution will work only for the applications that connect to the database.
Thanks & Regards,
Pramod
|
|
|
|