I have the following code:
class SearchCriteria
{
public string Name { get; set; }
public string Email { get; set; }
public string Company { get; set; }
// ... around 20 fields follow
public void Trim()
{
if( ! String.IsNullOrEmpty( Name ) )
{
Name = Name.Trim();
}
if( ! String.IsNullOrEmpty( Email ) )
{
Email = Email.Trim();
}
// ... repeat for all 20 fields in the class.
}
}
I want to write one function that will properly trim the fields, something like:
public void Trim()
{
Trim( Name );
Trim( Email );
// ...
}
private static void Trim( ref string field )
{
if( ! String.IsNullOrEmpty( field ) )
{
field = field.Trim();
}
}
Of course, this is not permitted in C#. One option I have is to write a helper and use reflection. Is there another way I can achieve this (reflecting on so many properties will deffinitely have a performance hit on that particular scenario and I can't afford that)?
If you already have the code, what are you asking? It's readable and efficient. But maybe it would be better to let the properties already trim the passed value in the first place.
class SearchCriteria
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value == null ? null : value.Trim(); }
}
private string _Email;
public string Email
{
get { return _Email; }
set { _Email = value == null ? null : value.Trim(); }
}
private string _Company;
public string Company
{
get { return _Company; }
set { _Company = value == null ? null : value.Trim(); }
}
// ... around 20 fields follow
}
Even if you could use a reflection approach. Consider that this code is always difficult to understand and to maintain. And it will silently trim properties even if they should not be trimmed. For example if another developer extends this class.
public void Trim()
{
Name = Trim( Name );
Email = Trim( Email );
// ...
}
private string Trim(string field )
{
if( ! String.IsNullOrEmpty( field ) )
field = field.Trim();
return field;
}
EDIT:
try also to apply Trim
fuction in setters of properties
class SearchCriteria
{
private string Trim(string field)
{
if( ! String.IsNullOrEmpty( field ) )
field = field.Trim();
return field;
}
private string _name;
public string Name
{
get { return _name; }
set { _name = Trim(value); }
}
private string _email;
public string Email
{
get { return _email; }
set { _email = Trim(value); }
}
// ... other string properties
// no public void Trim() method
}
Seems overkill .. saved the time in Trim()
, wasted the time in field declaration
class SearchCriteria
{
private Dictionary<string, string> _internalValues = new Dictionary<string, string>();
public string Name { get { return _internalValues.ContainsKey("Name") ? _internalValues["Name"] : null; } set { _internalValues["Name"] = value; } }
....
public void Trim()
{
foreach (var entry in _internalValues)
{
if (!string.IsNullOrEmpty(entry.Value)) _internalValues[entry.Key] = entry.Value.Trim();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With