Question

How can I put a string to the next empty index in a string array? I want to use a foreach loop and see if all the boolean strings are valid bools, and then put the keys of the invalid boolean strings to an array

string[] invalidKeys;
foreach (string key in ConfigurationManager.AppSettings)
{
    string value = ConfigurationManager.AppSettings[key];
    if (IsValidBooleanString(value) == false)
    {
        //Add 'key' to next empty index in the array 'invalidKeys'.
    }
return invalidKeys;
Was it helpful?

Solution

You haven't initialized or specified the length of the array. You need to specify the length for the Array to create one, but in your case you don't know that info in advance

So you can use a List instead

var invalidKeys = new List<String>();
foreach (string key in ConfigurationManager.AppSettings.Keys)
{
    string value = ConfigurationManager.AppSettings[key];
    if (IsValidBooleanString(value) == false)
    {
        //Add 'key' to next empty index in the array 'invalidKeys'.
        invalidKeys.Add(key);
    }
return invalidKeys;

Also noticed your foreach should be on ConfigurationManager.AppSettings.Keys not ConfigurationManager.AppSettings

Other way of doing this will be

var invalidKeys = 
ConfigurationManager.AppSettings.Keys
.Where(k => IsValidBooleanString(ConfigurationManager.AppSettings[k]) == false)
.ToArray();

OTHER TIPS

If you really want to become a C# programmer:

return (from string key in ConfigurationManager.AppSettings
    where !IsValidBooleanString(ConfigurationManager.AppSettings[key])
    select key).ToList();

Or ToArray() or whatever.

The functionality of IsValidBooleanString() may be useful elsewhere. I would recommend placing it in an extension class:

public static class StringExtender
{
  static readonly string[] validBooleanStrings = { "True", "False", "Yes", "No" };

  public static bool IsValidBooleanString(this string value)
  {
    return ValidBooleanStrings.Contains(value, StringComparer.OrdinalIgnoreCase);
  }
}

Some will say it's overkill to create an extension class for a single method, but it's really not and it makes your code modular and reusable. Not to mention you can add more extension methods here as you encounter similar scenarios.

Now all string objects have a .IsValidBooleanString() method attached to them and automatically pass themselves (this) to it. MSDN: Extension Methods (C#)

For retreiving a list of all setting keys with invalid boolean string values, I would use a LINQ query:

var settings = ConfigurationManager.AppSettings;

// Gets all the keys for values that are invalid boolean strings.
var invalidKeys = from key in settings.Keys
                   where !settings[key].IsValidBooleanString()
                   select key;

 // If you want a list...
 var invalidKeyList = invalidKeys.ToList<string>();

 // If you want an array...
 var invalidKeyArray = invalidKeys.ToArray<string>();

LINQ queries return IEnumerable<> values based on what you give it. Since I am selecting on a string type (key), it knows to give me back an IEnumerable<string>. You can then create a List<string> or string[] from the results if you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top