|
Yes I did Google search but there problem. I am not asking only object serialization. I want to ask is it possible to serialize the object of the form in which I am working. and when I open this form again I want to get the saved object of my form.
Please give me guild line.
|
|
|
|
|
As davey said you have to use serialization which isn't an easy thing to do.
I would start with something simpler (if you'v never done it before) and work my way up to a form. (start with just a control or something like that)
Per definition you can serialize everything (as far as I know) so yes it's possible to serialize a form, but probably not going to be easy.
|
|
|
|
|
Yes I did serialization before but I never save Controls or form.
Now I have to save the full form object or I will save all the form controls and get there properties and then put them to another serialize class then I will save it(It is very lengthy process). But I am in search of short cut. Like this.
SaveObject(this);
MyClass clss =Retriveobject();
Is it possible.
If yes then how I am stucked.
|
|
|
|
|
it is possible but you'll have to serialize every object on the form, then serialize the form itself.
I have some code at home that does something like this I'll check it when I get home.
But don't count on it being as easy as you typed there, not going to be possible to do in 2 lines of code
|
|
|
|
|
It can be done but it's going to be slow! You'll need to use reflection to go through every property that you want to serialize. When you get to properties like Control, you'll have to do the same for every property of each control in the collection... There may be a better method but that's all I can think of.
This little example will get you started. I've hard coded a few properties, but to make it a complete serialization you'll need to use reflection to get the properties/values instead as I said above.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace FormSerializationDemo
{
[Serializable]
public partial class Form1 : Form, ISerializable
{
public Form1()
{
InitializeComponent();
using (MemoryStream stream = new MemoryStream())
{
SerializeMe(stream);
}
}
private void SerializeMe(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("FormBorderStyle", FormBorderStyle);
info.AddValue("Location", Location);
info.AddValue("Name", Name);
info.AddValue("Size", Size);
info.AddValue("Text", Text);
}
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Thanks for your reply and providing code.
It is a good example but I think it will not solve my problem. Because I have an array of panels. These panels contain Four type of controls but number of controls is vary in each panel.
with the your technique I think I cant add different panels with different controls.
Any way thank you very much.
|
|
|
|
|
If you read the text and not the code the solution is in there. Reflection and recursion.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I've not tested it but you might be able to modify the following code; this basically does a deep copy of an object using serialization. Some minor modifications should allow to to write to a FileStream rather than a MemoryStream and then you'd extrapolate two functions (get and set).
<br />
public static T DeepCopy<t>(T obj)<br />
{<br />
object result = null;<br />
<br />
using (var ms = new MemoryStream())<br />
{<br />
var formatter = new BinaryFormatter();<br />
formatter.Serialize(ms, obj);<br />
ms.Position = 0;<br />
<br />
result = (T)formatter.Deserialize(ms);<br />
ms.Close();<br />
}<br />
<br />
return (T)result;<br />
}<br />
<br />
</t>
I haven't got time to work through the solution fully so I've left it as an exercise for you.
It definitely isn't definatley
modified on Thursday, January 22, 2009 6:18 AM
|
|
|
|
|
Thanks for your code but the line
formatter.Serialize(ms, obj);
gives the following exception. I marked the class as [Serializable].
Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
|
|
|
|
|
Hi All,
Actually I've coded a small windows application which takes up a dll and then using reflection finds out the various classes and properties in it (just like an object browser ) and initializes them at run time. I've used recursion to do this for initializing inner classes. But for a large dll which has many innerclasses and properties the application is throwing 'Stack Overflow exception'.
Is there any way to avoid it ? or the alternate way instead of using recursion?
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
If you're getting a stack overflow then, chances are, you have gone into an infinite loop into your recursion. You may need to trace through (tedious I know), and identify where the loop is kicked off - it could be down to a circular dependency going on in there.
|
|
|
|
|
If your recursive function accepts a lot of parameters, or even just one or two string parameters, you can overflow the stack rather quickly. Back in the day, the stack was only 64K big, but I don't know if that's still the case, or if the stack size can be increased. Each function call requires a certain amount of overhead, and having a recursive function that tacks strings requires as much stack space as the string(2) requires in memory (in addition to the regular overhead needed by the function call itself).
If it were me, I would look for a solution that didn't require recursion (if possible), or abstract out the recursion so that you don't have to make so many recursive calls before exiting the loop.
.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: the stack was only 64K big, but I don't know if that's still the case, or if the stack size can be increased.
The default reserved stack size is 1 MB. You can change it for any executable using editbin.exe /STACK
|
|
|
|
|
Imagine that there might be an extreme case where there is a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class in a class, but that is still very very far from causing a stack overflow. If your method has a huge amount of local variables, you might use a few kilobytes out of the megabyte of stack space that is available.
So, there has to be something wrong with your recursion...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi,
I think this is the issue that is causing trouble...but i don't find any other way barring recursion which can easily navigate into the inner classes...The dll's in fact contain many number of innerclasses and properties... so is there any solution for this??
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Spunky Coder wrote: I think this is the issue that is causing trouble...
I very much doubt that....
Spunky Coder wrote: The dll's in fact contain many number of innerclasses and properties...
I have several times used a class in a class, perhaps sometime a class in a class in a class. I could imagine a reason to do perhaps four or five levels, but hardly more than that. Do you say that the assembly contains classes that are nested inside each other to something like hundred levels?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
There is probably something going wrong in the recursion as the nesting should not really get that deep. When the exception occurs take a second to look at the stack trace in the debugger and get an idea of how deep the recursion got. However, to answer your question the alternate of recursion is to use and manage a Stack yourself. Using the Generic Collections from .NET 2.0, you can use Stack<T> for this purpose. It's really the same concept as recursion because in recursion you're using the call stack and the system is managing it for you. Anyhow, there are bunches of good articles on the internet of how to mimic recursion with a stack; off-hand I found this one:
http://haacked.com/archive/2007/03/04/Replacing_Recursion_With_a_Stack.aspx[^]
Hope that helps!
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Hi,
Thanks for the reply...I have gone through the article but seems that it doesn't work if there are any circular references. But in the dll's i use, circular reference exist. So is this kind of situation can be solved ??
Also is it a good thing to increase the stack size ??
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Spunky Coder wrote: But in the dll's i use, circular reference exist.
Class definitions can not be circular. A class can not be defined inside itself...
Spunky Coder wrote: Also is it a good thing to increase the stack size ?
You have something around a megabyte of stack space, what you are doing should typically use something like 0.1% of that. If you run out of stack space, it's not the size of the stack that is the problem.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I generally follow this pattern to prevent circular references. Create a List<t></t> that will accompany your stack. This will contain the items that have already been processed. At the top of your stack loop, the first step is to pull the next item of the stack and the second step should be to check that item against the list of items already processed. If it has already been processed, discard it and go to the next item. I generally put this in place even when the data SHOULDN'T have circular references, but I prefer to be safe rather than deal with some difficult to find bug later on.
Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.
|
|
|
|
|
|
Tuntgerhuu wrote: Help me
We all are with you. Go on, friend.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi,
I've created WCF application, from WCF client application i'm calling service application (basically Windows Service) methods but i'm not able to receive service response there.
If I call from multi thread environment then I'm getting response, what should be done for NON-Multi threading call?
Thanks
|
|
|
|
|
I can use String.IsNullorEmpty() to test a string for null. How can I test a string for DBnull please?
|
|
|
|
|
Hi,
you can use
<br />
if(myValue != DBNull.Value) {<br />
...<br />
<br />
}<br />
for the check if the value equals the value DBNull.
Regards
Sebastian
|
|
|
|