CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2008
    Posts
    29

    load settings.settings in an array and datagrid

    I am trying to load the properties settings.settings contents in a datagrid so it can be modified and the saved to the settings.settings file.

    I would like to do something elegant and extensible so when I add new settings I dont have to change all the form code.

    So basically I do not want to use something like xxx= Settings.Default.Param1.ToString() but rather do a loop and put it all in an array and put it in the datagrid.

    After several hours of googling I still can't find a way to do that loop, I can get the number of items in settings.settings using Settings.Default.Properties.Count but not address them in a loop using foreach or something like Settings.Default.[i].

    Any help or pointers welcome.

    Ps: I'd like to keep it simple using C# methods and functions rather than reading the file and doing de-serialisation.

  2. #2
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Thumbs up Re: load settings.settings in an array and datagrid

    Quote Originally Posted by Otex View Post
    I am trying to load the properties settings.settings contents in a datagrid so it can be modified and the saved to the settings.settings file.

    I would like to do something elegant and extensible so when I add new settings I dont have to change all the form code.

    So basically I do not want to use something like xxx= Settings.Default.Param1.ToString() but rather do a loop and put it all in an array and put it in the datagrid.

    After several hours of googling I still can't find a way to do that loop, I can get the number of items in settings.settings using Settings.Default.Properties.Count but not address them in a loop using foreach or something like Settings.Default.[i].

    Any help or pointers welcome.

    Ps: I'd like to keep it simple using C# methods and functions rather than reading the file and doing de-serialisation.
    This will fill the array from your settings.
    Inorder to work this, you have to create properties with "Param1", "Param2", "Param3" and so on.

    Code:
    string[] arr = newstring[Properties.Settings.Default.Properties.Count];
    for (int i = 0; i < Properties.Settings.Default.Properties.Count; i++)
    {
    arr[i] = Properties.Settings.Default["Param" + i.ToString()].ToString();
    }
    
    Regards,
    MMH
    Rate my post if you find it usefull.

  3. #3
    Join Date
    Dec 2008
    Posts
    29

    Re: load settings.settings in an array and datagrid

    Thanks MMH, this is a nice and simple but having all my settings called ParamXX would definitely make the code difficult to read.

    Perhaps I get some other ideas.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: load settings.settings in an array and datagrid

    Quote Originally Posted by Otex View Post
    Thanks MMH, this is a nice and simple but having all my settings called ParamXX would definitely make the code difficult to read.

    Perhaps I get some other ideas.
    Serialization to an XML file and deserialization are only a few lines of code and you will have all the properties names of your dataclass used. This is a secure and easy way to handle your setup data and keeps your data and code readable.

    You can always do it a more complicated way, but I think nothing is easier then to serialize a class which contains your setup data.
    What probems do you have using an XMLfile and serialization ?

    Simple the idea to have different names for Properties but then loading them all into &#243;ne array sounds strange to me. If you need an array of the same kind of data the settings provides you also the possibility for that. An XML file ghas the big advantage you also can edit it by hand and outside your program if needed. This is especially a good property of XML files when you are still in the design or testing envireoment.
    Last edited by JonnyPoet; May 1st, 2009 at 04:49 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Dec 2008
    Posts
    29

    Re: load settings.settings in an array and datagrid

    In fact I am already using serialization to store my configuration, this was an attempt to to use more standard ways to do the things using the tools built into the environement.

    The problem is that I had some experience programming in C and various assembly several years ago, I am talking the time when Borland just made the Turbo C++ compiler.
    I am now coding an application in C#, even tho it works I can clearly see that it is more structured programming than OOP, I really really struggle to switch my mind to OOP and more modern techniques but I am trying to change things "the way ppl do it"

    For this particular case my answer was provided in another forum (sorry I was not getting luck here)

    foreach (System.Configuration.SettingsProperty property in Properties.Settings.Default.Properties)

    Thanks

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: load settings.settings in an array and datagrid

    Quote Originally Posted by Otex View Post
    ...
    foreach (System.Configuration.SettingsProperty property in Properties.Settings.Default.Properties)
    Thanks
    Hmm but in this case you would also have a need to know what sort the specific property would be or do you only have one type of value there, like only strings, only integers, ..
    But its great that you could find a solution that fits your needs.
    ..I am now coding an application in C#, even tho it works I can clearly see that it is more structured programming than OOP, I really really struggle to switch my mind to OOP ...
    Maybe some books about design patterns may help to overcome this. Like W. Cooper's C# Design Patterns A Tutorial. Easy to read, good starter gradient IMHO
    Last edited by JonnyPoet; May 3rd, 2009 at 02:07 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured