0

I've created 32 properties.settings type of int32[,]. I will use them during runtime-read and write some data and check every settings values with foreach command. i get some problems iterating through properties values. Here is my code:

 private void button1_Click(object sender, EventArgs e)
 {
     foreach (SettingsProperty c in Properties.Settings.Default.Properties)
     {
         if (c[0,0]==0)     // i can not reach this byte :(
         {
                c[0, 0] = 1;   // :((
         }
     }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138

2 Answers2

0

Unfortunately, the other solution will run, but is incorrect. You can confirm this by comparing:

Properties.Settings.Default.Properties[c.name].DefaultValue and Properties.Settings.Default[c.name], and find that they are different if the property has been assigned a new value -- even if it's been saved.

DefaultValue does not store the current value; only the default value in the global scope

To get the actual value, you have to iterate over Properties.Settings.Default.PropertyValues. Like so:

foreach(SettingsPropertyValue value in Properties.Settings.Default.PropertyValues )
{
  if (value.PropertyValue[0,0] == 0)
  {
    value.PropertyValue[0, 0] = 1;
  }
}
mostlydev
  • 715
  • 6
  • 16
-1

The value of a SettingsProperty is stored in its DefaultValue property. Try the following:

private void button1_Click(object sender, EventArgs e)
{
  foreach (SettingsProperty c in Properties.Settings.Default.Properties)
  {
    if (c.DefaultValue[0,0] == 0)
    {
      c.DefaultValue[0, 0] = 1;
    }
  }
}

You might also want to simplify the code using Linq:

private void button1_Click(object sender, EventArgs e)
{
  foreach (SettingsProperty c in Properties.Settings.Default.Properties
                                                .Cast<object>()
                                                .Where(c => ((int[,])((SettingsProperty)c).DefaultValue)[0, 0] == 0))
  {
    c.DefaultValue[0, 0] = 1;
  }
}

Or even better in one line of code:

private void button1_Click(object sender, EventArgs e)
{
  Properties.Settings.Default.Properties.Cast<object>()
                                        .Where(c => ((int[,])((SettingsProperty)c).DefaultValue)[0, 0] == 0)
                                        .ToList()
                                        .ForEach(c => ((int[,])((SettingsProperty)c).DefaultValue)[0, 0] = 1);

  // .ToList() is added because .ForEach() is not available on IEnumerable<T>
  // I added .Cast<object>() to convert from IEnumerable to IEnumerable<object>. Then I use the cast to SettingsProperty so you can use the DefaultValue.
}

Finally, this question might be helpful: C# How to loop through Properties.Settings.Default.Properties changing the values

Community
  • 1
  • 1
Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93
  • Moslem, thank you so much for your reply. When i write your offers to my code page, it says that: "System.Configuration.SettingsPropertyCollection" does not contain a definition for "where" and no extension method "where"... Do i make a mistake? Thank you so much... – Murat Uzunefe Sep 27 '15 at 21:02
  • My bad. `SettingsPropertyCollection` derives from `IEnumerable` not from `IEnumerable`. I will edit my post shortly to include the hack for that. – Moslem Ben Dhaou Sep 27 '15 at 21:16
  • Why it gives error all the time? It says:" Can not apply indexing with [] to an expression of type 'object' ". Thanks... – Murat Uzunefe Sep 28 '15 at 04:51
  • Because the cast must be to `int[,]` not to `int`. Also try to understand the code instead of just copy/pasting. The last error is very trivial and you should have fixed it your self. (Try now with updated code) – Moslem Ben Dhaou Sep 28 '15 at 08:10
  • Moslem, thank you so much for your great help. It also let me to learn Linq issue... Take care... – Murat Uzunefe Sep 29 '15 at 07:33
  • If the answer is helpeful then please upvote and accept it. – Moslem Ben Dhaou Sep 29 '15 at 07:34
  • Moslem, i tried so hard but could not succeed to make your first offer work. It always says me the same thing: " Can not apply indexing with [] to an expression of type 'object' " – Murat Uzunefe Sep 29 '15 at 17:44
  • It does not appear that DefaultValue stores the current value. – mostlydev Apr 05 '16 at 15:45