G
Guest
Hi,
I have two simple classes called 'User' and 'Users', the entire code for
both classes is shown below.
****======== User.cs ========****
public class User
{
private string _displayName, _email;
private bool _approved;
public User(string displayName, string email, bool approved)
{
this._displayName = displayName;
this._email = email;
this._approved = _approved;
}
public string DisplayName
{
get { return _displayName; }
}
public string Email
{
get { return _email; }
}
public bool Approved
{
get { return _approved; }
}
}
****======== Users.cs ========****
public class Users : System.Collections.CollectionBase
{
public User this[int index]
{
get { return (User)(List[index]); }
set { List[index] = value; }
}
public Users FilterByDisplayName(string criteria, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (_user.DisplayName.IndexOf(criteria) >= 0)
_tmp.Remove(_user);
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (_user.DisplayName.IndexOf(criteria) >= 0)
_tmp.Add(_user);
}
}
return _tmp;
}
public Users FilterByEmail(string criteria, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (_user.Email.IndexOf(criteria) >= 0)
_tmp.Remove(_user);
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (_user.Email.IndexOf(criteria) >= 0)
_tmp.Add(_user);
}
}
return _tmp;
}
public Users FilterByApproved(bool criteria, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (_user.Approved == criteria)
_tmp.Remove(_user);
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (_user.Approved == criteria)
_tmp.Add(_user);
}
}
return _tmp;
}
public Users Clone()
{
Users _tmp = new Users();
foreach (User _user in this)
{
_tmp.Add(_user);
}
return _tmp;
}
public int Add(User value)
{
return List.Add(value);
}
public void Remove(User value)
{
List.Remove(value);
}
}
On the 'Users' class I have three methods (FilterByDisplayName,
FilterByEmail and FilterByApproved) which basically do the same thing except
they work on a different field on the 'User' class, and in the case of
FilterByApproved on a different data type (bool instead of string). I cannot
find an elegant way to combine these three methods into one method called
Filter(), is there an elegant way of combining these three methods into one
using C# 2003?
Many Thanks,
Peter
I have two simple classes called 'User' and 'Users', the entire code for
both classes is shown below.
****======== User.cs ========****
public class User
{
private string _displayName, _email;
private bool _approved;
public User(string displayName, string email, bool approved)
{
this._displayName = displayName;
this._email = email;
this._approved = _approved;
}
public string DisplayName
{
get { return _displayName; }
}
public string Email
{
get { return _email; }
}
public bool Approved
{
get { return _approved; }
}
}
****======== Users.cs ========****
public class Users : System.Collections.CollectionBase
{
public User this[int index]
{
get { return (User)(List[index]); }
set { List[index] = value; }
}
public Users FilterByDisplayName(string criteria, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (_user.DisplayName.IndexOf(criteria) >= 0)
_tmp.Remove(_user);
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (_user.DisplayName.IndexOf(criteria) >= 0)
_tmp.Add(_user);
}
}
return _tmp;
}
public Users FilterByEmail(string criteria, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (_user.Email.IndexOf(criteria) >= 0)
_tmp.Remove(_user);
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (_user.Email.IndexOf(criteria) >= 0)
_tmp.Add(_user);
}
}
return _tmp;
}
public Users FilterByApproved(bool criteria, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (_user.Approved == criteria)
_tmp.Remove(_user);
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (_user.Approved == criteria)
_tmp.Add(_user);
}
}
return _tmp;
}
public Users Clone()
{
Users _tmp = new Users();
foreach (User _user in this)
{
_tmp.Add(_user);
}
return _tmp;
}
public int Add(User value)
{
return List.Add(value);
}
public void Remove(User value)
{
List.Remove(value);
}
}
On the 'Users' class I have three methods (FilterByDisplayName,
FilterByEmail and FilterByApproved) which basically do the same thing except
they work on a different field on the 'User' class, and in the case of
FilterByApproved on a different data type (bool instead of string). I cannot
find an elegant way to combine these three methods into one method called
Filter(), is there an elegant way of combining these three methods into one
using C# 2003?
Many Thanks,
Peter