A
Artie
Hi,
We have some test code where we would want to invoke a method with
optional parameters.
In C# 4.0, I believe we could use named parameters, but we're using C#
2.0
I found a Dictionary solved my problem, but I'm not sure it's the best
pattern for C# 2.0?.
Could anyone please comment on whether or not there's a more
appropriate pattern?
Cheers
artie
EXAMPLE:
public void AddToDatabase(Dictionary<string,object> fields)
{
string _surname = "";
string _city = "";
int _employeeId;
if (fields.ContainsKey("surname") _surname =
(string)fields["surname"];
if (fields.ContainsKey("city") _city = (string)fields["city"];
if (fields.ContainsKey("employee_id") _employeeId =
(int)fields["employee_id"];
....
// assemble SQL with the above parameters and execute SQL to write
to the database
....
}
USE:
public void Test()
{
....
Dictionary<string, object> fields = new Dictionary<string, object>();
fields.Add("surname", _localSurnameVariable);
....
fields.Add("city_id", _localCityVariable);
AddToDatabase(values);
....
}
public void AnotherTest()
{
....
Dictionary<string, object> fields = new Dictionary<string, object>();
// doesn't care about any other attributes, so only adds the
employee_id
fields.Add("employee_id", _localEmployeeId);
....
AddToDatabase(values);
....
}
We have some test code where we would want to invoke a method with
optional parameters.
In C# 4.0, I believe we could use named parameters, but we're using C#
2.0
I found a Dictionary solved my problem, but I'm not sure it's the best
pattern for C# 2.0?.
Could anyone please comment on whether or not there's a more
appropriate pattern?
Cheers
artie
EXAMPLE:
public void AddToDatabase(Dictionary<string,object> fields)
{
string _surname = "";
string _city = "";
int _employeeId;
if (fields.ContainsKey("surname") _surname =
(string)fields["surname"];
if (fields.ContainsKey("city") _city = (string)fields["city"];
if (fields.ContainsKey("employee_id") _employeeId =
(int)fields["employee_id"];
....
// assemble SQL with the above parameters and execute SQL to write
to the database
....
}
USE:
public void Test()
{
....
Dictionary<string, object> fields = new Dictionary<string, object>();
fields.Add("surname", _localSurnameVariable);
....
fields.Add("city_id", _localCityVariable);
AddToDatabase(values);
....
}
public void AnotherTest()
{
....
Dictionary<string, object> fields = new Dictionary<string, object>();
// doesn't care about any other attributes, so only adds the
employee_id
fields.Add("employee_id", _localEmployeeId);
....
AddToDatabase(values);
....
}