Azavea.Open.Common.Config.ParameterExists C# (CSharp) Method

ParameterExists() public method

Method to check if a parameter exists, prior to calling GetParameter (which throws exceptions if you request an invalid parameter).
public ParameterExists ( string component, string parameter ) : bool
component string The component or section of the config file, used to /// locate the parameter.
parameter string The name of the config parameter.
return bool
        public bool ParameterExists(string component, string parameter)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component", "Component cannot be null.  Parameter was '" +
                    parameter + "'.");
            }
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter", "Parameter cannot be null.  Component was '" +
                    component + "'.");
            }
            try
            {
                if (_paramsByComponent.ContainsKey(component))
                {
                    if (_paramsByComponent[component].ContainsKey(parameter))
                    {
                        return true;
                    }
                }
                return false;
            }
            catch(Exception ex)
            {
                ReThrowException("Unable to check if parameter exists.",
                    new object[] { ConfigFile, component, parameter }, ex);
                // that throws, but the compiler wants a return statement.
                return false;
            }
        }

Usage Example

 /// <summary>
 /// Get the config for characters to be removed if the defaults aren't good enough.
 /// </summary>
 /// <param name="config">The config file to use.</param>
 /// <param name="component">The component to use.  Only one parameter is
 /// available and it's optional: CharactersToRemove</param>
 public SpecialCharacterRemover(Config config, string component)
 {
     if (config.ParameterExists(component, "CharactersToRemove"))
     {
         _charactersToRemove = config.GetParameter(component, "CharactersToRemove");
     }
 }
All Usage Examples Of Azavea.Open.Common.Config::ParameterExists