Home » Community » Newbie corner » LoadFromFile, StoreToFile and Serialize
LoadFromFile, StoreToFile and Serialize [message #28959] |
Wed, 29 September 2010 12:39 |
281264
Messages: 270 Registered: June 2010 Location: Spain
|
Experienced Member |
|
|
Hi,
I have a simple query regarding how the referred functions work. I am attaching an easy example with the objective to save and retrieve the last size and pos of the main window. Bu t it does not work. Why?
In general terms, which is the best way to save/retrieve an application’s values?
Thank you.
Cheers,
Javier
|
|
|
Re: LoadFromFile, StoreToFile and Serialize [message #28962 is a reply to message #28959] |
Wed, 29 September 2010 13:42 |
|
Hi Javier,
You actually have it all correct, there is only mistake in your main. You create temporary instance of the window when calling each function, so it works on that, but it appears like doesn't work because Run() is called on another instance, which did not get the deserialized data.
To fix it, just create a variable for the window. It should look like this:GUI_APP_MAIN
{
prueba_crear_proyecto crear_proyecto;
LoadFromFile(crear_proyecto,ConfigFile());
crear_proyecto.Run();
StoreToFile(crear_proyecto,ConfigFile());
}
Best regards,
Honza
|
|
|
|
Re: LoadFromFile, StoreToFile and Serialize [message #28969 is a reply to message #28964] |
Wed, 29 September 2010 17:33 |
|
281264 wrote on Wed, 29 September 2010 13:59 | 1.- what is the actual difference between creating an object vs working with a temporary instance? Are there other more meaningful implications? What do you do in your applications?
|
Well, in both cases you create an object, the only difference is the syntax. E.g.:class something{
something(){...}
...
}
main(){
something s; //this creates variable of type something (the call to the constructor is implicit)
something(); //this is like calling a function, which returns object of type something
//(or you can think of anonymous object - it exist, you just don't know how is it called)
something t=something(); //you can either store it in variable of appropriate type
something().Run(); //or call some of its methods (but in this case you didn't save
//the reference to the variable anywhere so you have no way to access it later)
} I personally use whatever is shorter. In simple cases, where I just need to create the window and Run() it then "mywin().Run()" is totally sufficient. But when I need to access it more times, then I have to use the longer syntax: "mywin mw; mw.SetSomething();mw.Run();...".
281264 wrote on Wed, 29 September 2010 13:59 | 2.- To save/retrieve application’s data/config/etc..is the combination of mentioned functions the best choice? Are there other options available?
| There is many options. Serialization is IMHO the simplest, as it requires only to implement Serialize(), which is very simple thanks to overloaded operator %. What is actually the best choice depends a lot on what you intend to do. For example to store window position or state of the application (e.g. what document is opened), the binary serialization is great. But if you want to save configuration, it is a good idea to make it human readable. For this you can either use Xmlize functions available in U++, or if you prefer plain text there are some classes to help you with this here on the forum. The final decision what format to use is always up to the designer of the application
Honza
[Updated on: Wed, 29 September 2010 17:37] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Thu Apr 18 23:10:27 CEST 2024
Total time taken to generate the page: 0.04411 seconds
|