I've got the following code where for some reason I'm getting a KeyNotFoundException even though I'm using a key that I had retrived a couple of lines above. Does anyone know of a situation where this wouldn't work? I'm stumped. BTW 'SchemaElementType is an enum.
public class DefaultValue
{
private Dictionary<Parameter, string> _params;
public DefaultValue(Dictionary<Parameter, string> parameters)
{
_params = parameters;
}
public string GetParameterValue(string name)
{
foreach(Parameter param in _params.Keys)
{
if(param.ParamName.Equals(name))
{
// **** Issue here ****
return _params[param];
}
}
return string.Empty;
}
}
[DataContract]
public class Parameter
{
#region Members
private Guid _guid;
private Guid _formulaGuid;
private string _name;
#endregion
#region Constructor
public Parameter(Guid guid, Guid formulaGuid, string name, SchemaElementType type)
{
ParamGuid = guid;
FormulaGuid = formulaGuid;
ParamName = name;
ParamType = type;
}
public Parameter()
{}
#endregion
#region Properties
[DataMember]
public Guid ParamGuid
{
get { return _guid; }
set { _guid = value; }
}
[DataMember]
public Guid FormulaGuid
{
get { return _formulaGuid; }
set { _formulaGuid = value; }
}
[DataMember]
public string ParamName
{
get { return _name; }
set { _name = value; }
}
[DataMember]
public SchemaElementType ParamType { get; set; }
#endregion
#region Overrides
public bool Equals(Parameter other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
bool result =other._guid.Equals(_guid);
result = result && other._formulaGuid.Equals(_formulaGuid);
result = result && Equals(other._name, _name);
result = result && Equals(other.ParamType, ParamType);
return result;
}
public override int GetHashCode()
{
unchecked
{
int result = _guid.GetHashCode();
result = (result*397) ^ _formulaGuid.GetHashCode();
result = (result*397) ^ (_name != null ? _name.GetHashCode() : 0);
result = (result*397) ^ ParamType.GetHashCode();
return result;
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Parameter)) return false;
return Equals((Parameter) obj);
}
#endregion
}