473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Better way to do this in C# 2003?

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

Nov 17 '05 #1
6 1226
Peter" <Pe***@discussions.microsoft.com> wrote in message
news:93**********************************@microso ft.com...
Hi,

I have two simple classes called 'User' and 'Users', the entire code for
both classes is shown below.


One thing I noticed was you could do something like this

if ((_user.DisplayName.IndexOf(criteria) >= 0) != Exclude)

As for combining them into one function you could create a comparison
function and pass this in as a delegate or an interface, similar to the
IComparer interface, see help on ArrayList.Sort

Michael
Nov 17 '05 #2
Hi Michael,

Thanks for the reply, I have tried passing in a delegate to a Filter method
and using this to call the function that actually does the filter. But this
still means I have to have the three methods that actually perform the
filtering, the delegate passed in would just point to one of the three
functions.

Regards,

Peter
"Michael C" wrote:
Peter" <Pe***@discussions.microsoft.com> wrote in message
news:93**********************************@microso ft.com...
Hi,

I have two simple classes called 'User' and 'Users', the entire code for
both classes is shown below.


One thing I noticed was you could do something like this

if ((_user.DisplayName.IndexOf(criteria) >= 0) != Exclude)

As for combining them into one function you could create a comparison
function and pass this in as a delegate or an interface, similar to the
IComparer interface, see help on ArrayList.Sort

Michael

Nov 17 '05 #3
"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
Hi Michael,

Thanks for the reply, I have tried passing in a delegate to a Filter
method
and using this to call the function that actually does the filter. But
this
still means I have to have the three methods that actually perform the
filtering, the delegate passed in would just point to one of the three
functions.


That's correct but the three functions are essentially different (2 are just
co-incidentally the same I'd suggest) and should probably be 3 seperate
functions. The other option is to pass in a delegate to the property on the
object and have the function smart enought to deal with the different return
types. Or have the properties on the object passed out of an indexer.

Michael
Nov 17 '05 #4
Hi Peter,
I would abstract out the idea of filtering from the collection class, so
that the logic for filtering lives inside a different object say a UserFilter
class, this class would have one method called Filter which takes a user as a
parameter. The filter method would then return true if the user met the
criteria and false otherwise. You would then have deriviations of this class
like NameFilter, EmailFilter and ApprovedFilter.

Your Users class would then have one function called Filter which then takes
a UserFilter as a parameter i.e. Filter(UserFilter filter).

See the example below (the formatting might be a bit messed up):

using System;

namespace ConsoleApplication11
{

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
User userMark, userBob, userFrank;

userMark = new User("mark", "da*********@gmail.com", true);
userBob = new User("bob", "bo*@example.org", true);
userFrank = new User("frank", "fr***@example.org", false);

Users userCollection = new Users();
userCollection.Add(userMark);
userCollection.Add(userBob);
userCollection.Add(userFrank);

NameFilter nameFilter = new NameFilter("mark");
Users usersFilteredByName = userCollection.Filter(nameFilter, true);

EmailFilter emailFilter = new EmailFilter("da*********@gmail.com");
Users usersFilteredByEmail = userCollection.Filter(emailFilter, true);

ApprovalFilter approvalFilter = new ApprovalFilter(true);
Users usersFilteredByApproval = userCollection.Filter(approvalFilter, true);

Console.ReadLine();
}
}
public abstract class UserFilter
{
protected string _criteria;

protected UserFilter(){}

protected UserFilter(string criteria)
{
_criteria = criteria;
}

public abstract bool Filter(User u);
}

public class NameFilter : UserFilter
{
public NameFilter(string criteria) : base(criteria)
{}

public override bool Filter(User u)
{
return u.DisplayName.IndexOf(_criteria) >= 0;
}
}

public class EmailFilter : UserFilter
{
public EmailFilter(string criteria) : base(criteria)
{}

public override bool Filter(User u)
{
return u.Email.IndexOf(_criteria) >= 0;
}
}

public class ApprovalFilter : UserFilter
{
private bool _approvalCriteria;
public ApprovalFilter(bool criteria)
{
_approvalCriteria = criteria;
}

public override bool Filter(User u)
{
return u.Approved == _approvalCriteria;
}
}

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; }
}
}
public class Users : System.Collections.CollectionBase
{
public User this[int index]
{
get { return (User)(List[index]); }
set { List[index] = value; }
}

public Users Filter(UserFilter filter, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (filter.Filter(_user))
{
_tmp.Remove(_user);
}
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (filter.Filter(_user))
{
_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);
}
}

}
Also, there was a small error in the code you published, in the
constructor you have:
public User(string displayName, string email, bool approved)
{
this._displayName = displayName;
this._email = email;
this._approved = _approved;
}
you need to change the appoved line to say:
this._approved = approved not this._approved = _approved
Hope that helps
Mark R Dawson
http://www.markdawson.org

"Peter" wrote:
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

Nov 17 '05 #5
Awesome, your solution is what the word elegant was invented for :->

Cheers,

Peter
"Mark R. Dawson" wrote:
Hi Peter,
I would abstract out the idea of filtering from the collection class, so
that the logic for filtering lives inside a different object say a UserFilter
class, this class would have one method called Filter which takes a user as a
parameter. The filter method would then return true if the user met the
criteria and false otherwise. You would then have deriviations of this class
like NameFilter, EmailFilter and ApprovedFilter.

Your Users class would then have one function called Filter which then takes
a UserFilter as a parameter i.e. Filter(UserFilter filter).

See the example below (the formatting might be a bit messed up):

using System;

namespace ConsoleApplication11
{

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
User userMark, userBob, userFrank;

userMark = new User("mark", "da*********@gmail.com", true);
userBob = new User("bob", "bo*@example.org", true);
userFrank = new User("frank", "fr***@example.org", false);

Users userCollection = new Users();
userCollection.Add(userMark);
userCollection.Add(userBob);
userCollection.Add(userFrank);

NameFilter nameFilter = new NameFilter("mark");
Users usersFilteredByName = userCollection.Filter(nameFilter, true);

EmailFilter emailFilter = new EmailFilter("da*********@gmail.com");
Users usersFilteredByEmail = userCollection.Filter(emailFilter, true);

ApprovalFilter approvalFilter = new ApprovalFilter(true);
Users usersFilteredByApproval = userCollection.Filter(approvalFilter, true);

Console.ReadLine();
}
}
public abstract class UserFilter
{
protected string _criteria;

protected UserFilter(){}

protected UserFilter(string criteria)
{
_criteria = criteria;
}

public abstract bool Filter(User u);
}

public class NameFilter : UserFilter
{
public NameFilter(string criteria) : base(criteria)
{}

public override bool Filter(User u)
{
return u.DisplayName.IndexOf(_criteria) >= 0;
}
}

public class EmailFilter : UserFilter
{
public EmailFilter(string criteria) : base(criteria)
{}

public override bool Filter(User u)
{
return u.Email.IndexOf(_criteria) >= 0;
}
}

public class ApprovalFilter : UserFilter
{
private bool _approvalCriteria;
public ApprovalFilter(bool criteria)
{
_approvalCriteria = criteria;
}

public override bool Filter(User u)
{
return u.Approved == _approvalCriteria;
}
}

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; }
}
}
public class Users : System.Collections.CollectionBase
{
public User this[int index]
{
get { return (User)(List[index]); }
set { List[index] = value; }
}

public Users Filter(UserFilter filter, bool exclude)
{
Users _tmp;
if (exclude)
{
_tmp = this.Clone();
foreach (User _user in this)
{
if (filter.Filter(_user))
{
_tmp.Remove(_user);
}
}
}
else
{
_tmp = new Users();
foreach (User _user in this)
{
if (filter.Filter(_user))
{
_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);
}
}

}
Also, there was a small error in the code you published, in the
constructor you have:
public User(string displayName, string email, bool approved)
{
this._displayName = displayName;
this._email = email;
this._approved = _approved;
}


you need to change the appoved line to say:
this._approved = approved not this._approved = _approved
Hope that helps
Mark R Dawson
http://www.markdawson.org

"Peter" wrote:
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
{

Nov 17 '05 #6
"Peter" <Pe***@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Awesome, your solution is what the word elegant was invented for :->


This is the IComparer solution I suggested.

Michael
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
7
by: Bura Tino | last post by:
Hi, Going forward, what's better to use <td height="20"> or <td style="height:20;">
4
by: bthrnds-ns2 | last post by:
hey guys--i need some expert advice! we have a Dell PE 2650 with 2GHz and 2MB RAM with 2000 Server. on it we have about 6 MSSQL DB's. a couple of the DB's are the back-end for a decent sized web...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
24
by: Faith Dorell | last post by:
I really donīt like C.You can write better programs in BASIC than in C, if you donīt like this language. I donīt understand how C became so popular, although much better programming languages...
11
by: Kevin D. Quitt | last post by:
At least, when you're hungry. -- #include <standard.disclaimer> _ Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up Per the FCA, this address may not be added to any...
22
by: smartwolf agassi via DotNetMonster.com | last post by:
I'm a C# language learner. I want to know which IDE is better for C# programing, Borland C#Builder or VS.net 2003? -- Message posted via http://www.dotnetmonster.com
3
by: Jarod | last post by:
Hey I am considering Win2003 Enterprise or Win XP professional as a OS to work with VS 2003. Is there any difference for a programer ? I think probably most of programmers are using WinXP Prof....
3
by: Jay | last post by:
Well just installed the new 7.1 compiler. Not bad I must say but I'm becoming increasingly frustrated with the help system. Problem: project didn't compile due to DX9 include directories not...
1
by: Arjen | last post by:
Hi, Sometimes I see this: (string)DataBinder.Eval(Container.DataItem, "Answer") And sometimes this: Eval(Container.DataItem, "Answer") What is better (faster/performance) when publishing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.