471,337 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Pattern/Best Practice question

Hello.

Is there a pattern or best practice for the following scenario?

I have a list of items I would like to compare. The number of items are
decided at runtime.

ObjectA, ObjectB, ObjectC......ObjectX

I want to process a comparison for each of these projects. So, for
example:

if ( ( ObjectA == "345") AND ( ObjectB == "345") AND ......)

I'm guessing a foreach loop of somekind but I'm trying to come up with
a best practice type of solution. Of course my difficulty is because
the number of objects isn't known until runtime.

TIA as always!!
-A

Feb 7 '06 #1
4 1678
Guy,

That's about the best you can do. If you are performing just an AND
logical operation, then you know you can exit the loop when any of the
comparisons return false. The same goes for using just OR logical
operations and any of them are true.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Guy Noir" <ah******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello.

Is there a pattern or best practice for the following scenario?

I have a list of items I would like to compare. The number of items are
decided at runtime.

ObjectA, ObjectB, ObjectC......ObjectX

I want to process a comparison for each of these projects. So, for
example:

if ( ( ObjectA == "345") AND ( ObjectB == "345") AND ......)

I'm guessing a foreach loop of somekind but I'm trying to come up with
a best practice type of solution. Of course my difficulty is because
the number of objects isn't known until runtime.

TIA as always!!
-A

Feb 7 '06 #2
> Hello.

Is there a pattern or best practice for the following scenario?

I have a list of items I would like to compare. The number of items
are decided at runtime.

ObjectA, ObjectB, ObjectC......ObjectX

I want to process a comparison for each of these projects. So, for
example:

if ( ( ObjectA == "345") AND ( ObjectB == "345") AND ......)

I'm guessing a foreach loop of somekind but I'm trying to come up with
a best practice type of solution. Of course my difficulty is because
the number of objects isn't known until runtime.

TIA as always!!
-A


You could delegate (not a .NET delegate altough that's a possiblity too)
the work to another class that is designed to deal with that situation.

I though this was an interesting question so I wrote a little sample to express
my thoughts. This or the original foreach loop is about the best I can do
without more context or code.

class Class1
{

[STAThread]
static void Main(string[] args)
{
SomeClass[] list = new SomeClass[]{new SomeClass(345), new SomeClass(2),
new SomeClass(0)};

SomeClassComparer comparer = new SomeClassComparer(list);

Console.WriteLine(comparer.CompareAndInteger(345)) ;
Console.WriteLine(comparer.CompareOrInteger(345));

Console.ReadLine();
}
}

/// <summary>
/// Meant to simulate your ObjectA, ObjectB, OjbectC, etc...
/// </summary>
class SomeClass
{
private int intField;

public SomeClass(int i)
{
intField = i;
}

public int IntProperty
{
get { return intField; }
set { intField = value; }
}
}

/// <summary>
/// The custom comparer class
/// </summary>
class SomeClassComparer
{
SomeClass[] list;

public SomeClassComparer(SomeClass[] list)
{
this.list = list;
}

public bool CompareAndInteger(int value)
{
foreach (SomeClass someClass in list)
{
if(someClass.IntProperty != 345)
{
return false;
}
}

return true;
}

public bool CompareOrInteger(int value)
{
foreach (SomeClass someClass in list)
{
if(someClass.IntProperty == 345)
{
return true;
}
}

return false;
}
}

Chris
Feb 7 '06 #3


Putting all the objects into a list and then iterating through them
will work for an AND

ArrayList widgets = new ArrayList();

widgets.Add(new Widget(1));
widgets.Add(new Widget(1));
widgets.Add(new Widget(1));
widgets.Add(new Widget(2));
bool result = true;

foreach (Widget wid in widgets) {
if (wid != (Widget)1) {
result = false;
break;
}
}

if (result) {
MessageBox.Show("All are 1");
}
else {
MessageBox.Show("Something not 1");
}

hth,
Alan.

Feb 7 '06 #4
Thanks for the responses. It certainly helps.
-A

Feb 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

136 posts views Thread by Matt Kruse | last post: by
8 posts views Thread by Charles Law | last post: by
12 posts views Thread by FluffyCat | last post: by
4 posts views Thread by Anastasios Hatzis | last post: by
reply views Thread by rosydwin | last post: by

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.