Save something in settingsfile or.. ? in C# - c#

I am implementing an application and I have a few lists with some stuff in it, which is always the same, but I don't want to implement this in my real logic stuff.
Is there any way to save these items in your application? I've read some things about saving these items in a settingsfile.
Is this the best way, or there are better ways? and how can I do this?

You can save it in the application settings file

Save in XML. You can bind directly to xml in wpf. See: http://joshsmithonwpf.wordpress.com/2007/06/04/binding-to-xml/

If the content of the list is supposed to never be changed you can make them private static fields in a static class the provides a mthod to create a new instance of the list.
If the content of the list is supposed to be changed, it is a good idea to store it in the application settings file.
If the content of the list is supposed to be changed per user you should save it in the user settings file.

Depending of the content itself, I like to load the content of list into a database table. For example, a list of school would be stored in a table calle T_Ref_Schools. The Ref indicated to me that those values can be changed over time but not often.

Related

How to update and add objects to a dynamically nested JSON?

I have a class which contains a list of its instances of the same class. This can get pretty nested and is fairly dynamic. This class has other members and therefore you can have many different cases / setups with different type of nestings.
I am trying to save it as JSON and deserialize when I need to access data.
Serializing is easy since it successfully navigates through the nesting and generates the appropriate jsons.
I am having trouble with updating. So when the session starts, I deserialize the json data that I have, I use this data when needed as a reference to load saved settings when certain elements are created.
My problem is when I want to add a new entry to the json or make updates.
To make updates, I can recursively loop through and find the item and make modifications it it but adding a new item is where I am having difficulty.
Say a few nestings down the line, I have added a new item, How can I add this to the saved JSON in the appropriate spot?
I would post my code but it spans several classes and I would need to post a lot of code.
Essentially, I have a root class and then subclasses of the same type added as children to the list and other items.
How do I determine where in the nestings the new item belongs?
I can find specific items by recursively seraching every branch for its unique ID and make edits to that item to update it but not sure how to dynamically add a new item to specific places throughout the nesting.
When first run, I just take the settings the user sets and store it as JSON.
Next time, when it is run, I deserialize the data I have and store it.
Based on what the user selects, I will load from the above step the settings for any item he selects.
If the user adds a new item, somewhere through the nestings, I need to be able to determine where, then I need to combine both the old JSON and the new JSON (ovveride old values and add new elements if any) then serialize and write to file.
I can't just serialize the new data and store it because then it would be missing the old data if it was not created in this session.
It is kind of confusing but hopefully I have been helpful enough.
Thanks

Unique identifier for Interop.ListObject from VSTO - C#

I need a way to insert (or use an already implemented property that could serve as) a unique identifier into a Microsoft.Office.Interop.Excel.ListObject instance.
The problem is that when I'm creating a new ListObject as:
var excelTable = worksheet.ListObjects.Add(ExcelInterop.XlListObjectSourceType.xlSrcExternal, DUMMY_CONNECTIONSTRING, false, true, cellRange);
I cannot rely on the Name property of excelTable to browse for it in the collection since the user could change the value of that property anytime afterwards.
After browsing trough the object properties I found nothing I could use out of the box (like a Tag property for example, which exists in a Microsoft.Office.Tools.Excel.ListObjecttype of object I cannot use at this point due to dependencies) ...and other weird stuff like a DisplayName that appears not only unable to be set directly but also to reflect the exact same value that the Name property has at all times (Why would you want to have 2 properties that reflect the same value at any time?).
I've thought on either creating my own implementation of this class or probably use the Comment property to store a GUID (which I don't know why kinda feels wrong):
excelTable.Comment = Guid.NewGuid().ToString();
Can you suggest of another way to accomplish this task?
Thanks a lot!
It is quite frustrating that there is no "Tag" (or similar) property that you could set on Excel objects. I'm facing the same issues as you. Here are two options that you can use:
alternative text property (for the table it is only visible by right clicking the table, selecting table and alternative text). This is probably a bit cleaner than Comment since the UI for comment is always visible.
you could also generate a wrapper object that contains a direct reference to the ListObject. So one wrapper object for each created ListObject. This works fine until you save / open the workbook again. If you need to be able to identify the table again after reopening the workbook you would still need to write some id to Comment or Alternative text. You could probably do a clean implementation by using events like BeforeSave and AfterSave (add alternative text before save so it saves to disk, then remove it again after save so that the user doesn't see it. When the workbook opens you load up your wrapper objects and remove the alternative text).

Create object names dynamically

I am building web service in C# for a particular application, I have a XML definition of module. I have created a class called Field that holds the properties of all fields on a module. What I would like to do is create the field objects but name them dynamically then add them to a list of some sort. So when I reference them from the client it would be like this:
Module.Fields.MyDynamicName.FieldProperty
Is this possible to do? and could anyone point me in the right direction on how to do this.
Hope my question makes sense.
Basically you need to design for "deferred design", which means you do not know at compile time what the design is, but you still need to accommodate it.
There are probably a few ways but what I have done in the past is use a dictionary list of Key/Value pairs to store fields. Using serialization (I prefer Json) you can shove just about anything into a string and store it as the Value, then deserialize it when you need it.

Reloading form without losing the variables values

When I click on a button my form needs to reload, without loosing some variables.
For example:
A game needs to be reset, but the high score variable needs to be kept to count with the next one.
"Application.Reset();" does reset the form1, but also all the variables.
Is there a way out without using databases, because we haven't learned that yet.
It can be achieved by
1.saving the data in Database
2.XML file (Write and Read)
3.Setting the properties of Class
4.Using the Static Variables**
You need to create a Class and have properties and methods in that and pass it to the form. Upon exit of the form, update your Class values with those that were changed on the form.
After that you will need to figure out "storing" the class in some sort of Stream and being able to reload your class from that stream. (XML, Binary, Protobuf, JSON) there are many ways.
You can use Settings:
http://msdn.microsoft.com/en-us/library/bb397750.aspx
and save your data on reset/closing

Need some advice for project

I'm going to do a project task as the last part of a programming course in C#, and I have choosen to do a media library for storing information of DVD movie collections. The thing I need some advice is about how to save this information since the task should be solved by using simple text files and not databases which had been better.
So, which is the best way to save this information to be able to add, edit and allow search for titles and actors and also sort after genre to mention some of the idéas I have for this program.
Should I just save the information by just add the title, year, playtime, director, actor1, actor2, actor3, genre, grade, comment for each movie lika a row at the end of the file? Can I search in the file or should I, in some way, first read it all into an array, and then do the serach and perhaps edit and save the complete file again?
Or is it better to use XML like this:
<movie_collections>
<movie>
<title=Tron Legacy"></title>
<year=2010></year>
<playtime=120></playtime>
etc.
</movie>
</movie_collections>
If I use XML, can I search for a title and just load or edit that part? Are there better alternatives than these?
You may store the data in XML file. An XML file can store data similar to a database table. You can add multiple records, hierarchical data etc... You may easily query the data using LINQ to XML.
If you dont want to use LINQ to XML, You can use so typical XMLDocument to handle the XML data.
Maybe you're approaching this at too low a level. The file is realistically just for persistance rather than implement your own DB as a file.
Just implement the object that you're after and serialize it out imo.
public class MovieCollections
{
private ICollection<Movie> Movies {get; set;}
// etc...
}
public class Movie
{
public string Title {get; private set;}
public int PlayTime {get; private set;}
public DateTime ReleaseDate {get; private set;}
}
Serialize instance of MovieCollections.
Keep MovieCollections perhaps as a Singleton instance since you only want one collection.
By the way this seems to be very general question & a quite common homework question too!!
I think you should google for this, you will get better projects ideas.
Something similar on this lines :
1.Simple Movie Database in C# using Microsoft Access
2. Create a Movie Database Application
3. imdbapi
& finally
4. SO's similar post
And as far as comparison between a database & XML is concern, I did recommend you a database because several pros over XML as far such type of project is considered.
As #M_Affifi suggests, think of all your interaction with your data (e.g. sort, add, find, delete, update, etc) through your objects which reside in memory (e.g. Instance of MovieCollections class and instances of Movie class), the XML file is only used to store the state of your objects, so you have that data available next time you run your application.
Once you're done manipulating your data just serialize it to XML. Serialization is just the process to convert your objects into a format that you can store or transfer, in this case will be conversion to XML. Take a look at this reference, Your serialization needs are very basic, so you'll need just few lines of code.
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(YourObject.GetType());
x.Serialize(Console.Out, YourObject)
Finally, I would suggest thinking of your application logic. When program starts you may want to check if your Movies.xml exists, if so, then 'Deserialize' it, which means loading it from XML to your object collection. If XML file doesn't exist then just start manipulating your objects and when you're done give the option to Save(Serialize). I can help with the serialization/deserialization process if needed.
Hope this is helpful
;

Categories