What is the best way to save the state of a program. Maybe that is not the right way to describe it but, what I mean is almost any application you can input a whole bunch of data make selections and choices and then save these in files unique to the application your working with.
For the time being I will ask my question in term of VB.NET since it is what I am currently working with. I understand the use of the stream writer to write data to a file (any file extension can be used even your own made up one) and then you can later open the file with the stream reader and load the saved application state. At least that is what I know how to do.
Are there other ways to approach saving the state? In my case I have a dictionary that is defined through user input to store a lot of data and I am trying to find the best way to save the dictionary so I can load it again.
I would suggest the best way to save state is the way that makes sense to you (and presumably fits in with your architectural style).
There are various locations and methods of encoding state into a file but, with the exception of a few extreme cases, there's unlikely to be any (user) perceivable performance differences between the techniques.
If one was feeling especially concerned about such things, it might be worth hiving off the reading or writing of state onto a background worker thread but I'd probably hold off on that if / until you actually start running into any disk bound perf issues.
You can actually do this using vs.net IDE and click your way to happiness. Click a control where you want to save the state, then expand the ApplicationSettings in properties. Then click the ... (box) by PropertyBinding. Now choose the property you want to store the setting for and click New. Now name your setting and select whether it is a per user setting or a application setting in the scope.
OMG, THAT IS IT, AMAZING!
Now, when you want to save the state of a windows form, just put in your code:
My.Settings.Save()
Related
I've been using the Game State Management sample which has worked so far. I've hit a snag though: when Tombstoning, the screens are serialised; the only trouble is, the MessageBoxScreen has event handlers for Accepted and Cancelled.
What's the best way to serialise these? I did a bit of research on using Expression Trees but this seemed overly complex for what I wanted to do.
How do you serialise these? Or... What alternative approach do you use to save the state of a screen that contains delegates?
I'd definitely steer clear of attempting to serialize anything remotely resembling a lambda, or for that matter, named methods. Remember: you're storing state, and nothing else.
Depending on how far and wide your various assignments to these delegates are, you might be able to get away with maintaining a Dictionary<String, WhateverDelagateType>, serializing the keys and looking up the callbacks after deserialization.
Another thing to consider--I'm no expert, but reading between the lines it sounds as if you're working towards tombstoning a very temporary modal dialog. Do you really want that? You might be better off bringing your user right to the high scores table, or whatever follows your dialog, on his/her return.
I decided against this. I instead persists game flow as a kind of 'flow chart'.
The flow chart is declared in code and has properties 'LastShape' and 'LastResultFromShape'.
In my code, I rebuild the flow chart definitions each time, something like this:
flowChart.AddShape( "ShowSplash" );
flowChart.AddLine( "MainMenu", ()=>lastResult=="Clicked" || lastResult=="TimedOut");
flowChart.AddShape( "MainMenu");
flowChart.AddLine( #"ShowOptions", ()=>lastResult=="OptionsClicked");
flowChar.AddLine( #"ShowSplash", ()=>lastResult==#"TimedOut");
etc.etc.
The flow goes from the top down, so 'AddLine' relates to the last shape added.
After tombstoning, I just read the last shape and the last result and decide where to go in the flowchart based on that.
Good morning all.
I'm relatively new to the Visual Basic realm (although a traditional web based script developer), i've come to ask you a question. I am reading data from an XML file. This local XML file will be updated by another application, and I will need to periodically re-evaluate the XML file, and only import new data into a list box. Furthermore, I want to be able to click on a particular item in the listbox, and display the other values about that particular XML entry.
So, I suppose this is a multi part question. What is the proper way to import only NEW data into the program, what is the proper way to store the data, and how do I associate a value in a listbox with the data stored elsewhere?
I've considered multidimensional arrays, but have been told that strings to char arrays and then back to strings is a terrible way to manage the data, but was never offered an alternative.
I will be satisfied with a list of topics to study up on and/or an example for an answer to this question.
I would probably use classes that implement INotifyPropertyChanged and a BindingList. Then you just need to listen to ListChanged events off of the list and update the list box then.
I have a blog post that discusses binding classes and interfaces if you want to learn more about them: Data Binding Classes, Interfaces, and Attributes in Windows Forms 2.0. It might be a little dated by now, I haven't reviewed it since I wrote it in March, 2007.
As a start look at the XmlDocument and XmlReader classes.
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
XmlDocument helps load a document into memory and allows you to look at the document in any way you desire, depending on the size of the file there may be implications as to how long pulling in the file takes
XmlReader allows access on the fly, and gives you access very much like a DataReader. I.e. keeping track of your position in the dataset and not retaining any data once you have inspected it.
For keeping a track of updates, it depends where the XML is stored.
If it is in a file a FileSystemWatcher may help in determining when you need to update....
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
I am extreamly new to programming. I am writing a simple game which gives you a score everytime you run the game. I am trying to figure out how to save the scores and recall them to a high score page. I am trying to figure out if saving to a dictionary is the best way, or using an array, or what. Plus, how does the dictionary handle an object that needs to be updated and changed, everytime the high score is beat?
well, the simplest way is just to store them in a text file. How this works is relatively simple in any programming language and most languages will have some documentation online that explains the functions for reading and writing from files. How to actually handle the values in memory (array, list, etc) will depend on the language you are using.
I'm new to Obj-C too, but from a pure programming point of view I'd agree with maqleod. You're problem is twofold: you need a data structure to hold the highscores in memory whilst your game is running. I think an NsMutableArray would serve you best, because it can sort itself for you (an NsDictionary is better when you want to lookup discrete values). On a higher level however, you need to save this data between sessions - data persistency. Thats where saving to a text file comes into play. If you look up / google "archiving objects" you'll find that it' really quite a trivial task in obj-c to save the contents of an object (eg. An array containing highscores) to a file - and of course initialize it again from file next time the game starts up.
I created this simple textpad program in WPF/VB.NET 2008 that automatically saves the content of the forms to an XML file on every keystroke.
Now, I'm trying to make the program see the changes on the XML file in realtime.. example, If I open two of my textpads, when I write on the first one, it will automatically reflect on the other textpad.
How can I do this?
One of my colleagues told me to read about iNotifyPropertyChanged (which I did) but how can I apply it to my application..?
:( help~
btw, I got the idea from a Google Wave demo, and I'm actually trying to do something bigger..
Note - this approach will be really, really expensive in terms of disk I/O, memory usage and CPU time. Why are you using XML is that the native format of the data you are editing? You may want to look at a more compact format - one that will use less memory, generate fewer I/Os and use less CPU.
Also note that you writer may need to flush the file for the watcher to notice any changes. This is expensive as well - especially if you re doing it on every key stroke.
Be sure to use the correct file open attributes (sharing, reading and writing).
You may want to consider using shared memory to communicate between your processes. This will be less expensive. You can avoid large ammounts of disk I/O by only writing changes to disk when the use asks to commit them, or there is a hint to do so. I suggest avoiding doing this on every key stroke.
Remember, your app needs to be a good system citizen and consume a reasonable amount of system resources. This is especially true running on netbooks and other 'low spec' systems.
You will probably need to use the FileSystemWatcher to watch the file on the disk rather than a property in the running instance of the application.
Or you could use some custom message passing between different instances of your application.
INotifyPropertyChanged isn't going to work for your application. That interfaced is used when data binding some element to a UI object.
Your best bet is going to be to attach a FileSystemWatcher to the file when you open it for editing. You can then use the change events to reload the file as needed in each instance of your application.
This will also load changes made from external editors.
It sounds like you are using file IO as a form of interprocess communication, if so, IMO you need to rethink your design, especially if you are doing something "bigger" than google wave (whatever bigger means in this context) as what you are proposing is terribly ineficient.
Do some searching on Interprocess communication and you will get a whole bunch of idea's #foredecker's idea (+1) of shared memory is a good possibility for example.
I'm trying to decide whether some application state, specifically the selected item in a list, should be stored in Core Data or NSUserDefaults.
Generally I believe that application preferences and state should persist in NSUserDefaults, and model-level data should persist elsewhere, say in Core Data. My model so far is:
Should the data be stored at all? If the user wouldn't reasonably expect it to be, then throw it out (for example, the cursor position is not saved in TextEdit)
NSUserDefaults:
If the application were multi-document, the setting would apply to all documents
It's conceivable that the data would be configured in preferences
Having the data outside of the model makes sense for testing (swapping several defaults with one model store)
Model-level
The data clearly belongs as an attribute of a model-level object
The data is sufficiently large that storing it in NSUserDefaults would cause performance problems
It would be difficult or time-intensive for the user to re-create the data (they would definitely consider the loss of this information "data loss")
I plan to store the sort order of some entities in Core Data. Without this information (i.e. a "sortIndex" or "order" attribute) each entity instance would have to be augmented with data from the user defaults.
However, storing state in the model seems like a slippery slope. If I store sort order then it also seems appropriate to store selection since they are both the state of a list. The selection data for my use case may actually be quite large. Specifically, the icons in one list depend on the selection in each of their sub-lists.
Does anyone have a hard line they draw with respect to NSUserDefaults vs. data model?
You didn't mention whether this is a document-based app (like say, TextEdit) or a library-based one (like say, AddressBook).
That may help you decide where such information should go: assume a document-based app. Assume its documents get placed under version-control (this is actually feasible when using Core Data's XML data store type). Open the app, change the doc's sort orders. Does this dirty the document? Would this change be worth a check-in? Would the change be valuable to other users of this repository?
Typically, sort orderings aren't valuable enough to warrant document-based storage (ala NSTableView's Auto Save Name in Interface Builder). But your app may place a priority on sorting (it sounds like it).
So, there is no hard-and-fast rule. But I think the idea of having a document under version control, potentially shared with others, provides a good intellectual framework to make your case for either side.
I agree with rentzsch, but another way to view it:
Is the selection part of the data or is it metadata? If metadata, is it metadata about a single document or is it state that should apply to any document that happens to be opened next?
Document-specific metadata might want to be stored as an extended attribute. For example, TextMate stores the selection for a document this way, much as BBEdit, MPW, and others used to store tab settings, window size, etc. as a resource in the resource fork. Metadata is considered optional and the document is intact if it is stripped away.
If the selection is an integral part of the data, then by all means store it in the data, using Core Data if you happen to swing that way.
And if it's not a document-based app, then NSUserDefaults is the simplest path since support for it is generally built into common NSView subclasses via bindings.
I personally don't have a hard line between saving preferences in the file itself or in NSUSerDefaults.
But I have always tended towards the obvious:
Application preferences = NSUSerDefaults
Document preferences = in the file itself
For selection state specifically, I would judge if keeping that is important enough to the user. If it is, and important enough to move with the document to another computer, I would keep it in the document itself.
If it isn't important (or applicable) I wouldn't bother with saving it at all.