Pattern/Best Practice question

G

Guy Noir

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

chris martin

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;
}
}

Chri
 
A

alantolan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top