> 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......Ob jectX
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)};
SomeClassCompar er comparer = new SomeClassCompar er(list);
Console.WriteLi ne(comparer.Com pareAndInteger( 345));
Console.WriteLi ne(comparer.Com pareOrInteger(3 45));
Console.ReadLin e();
}
}
/// <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 SomeClassCompar er
{
SomeClass[] list;
public SomeClassCompar er(SomeClass[] list)
{
this.list = list;
}
public bool CompareAndInteg er(int value)
{
foreach (SomeClass someClass in list)
{
if(someClass.In tProperty != 345)
{
return false;
}
}
return true;
}
public bool CompareOrIntege r(int value)
{
foreach (SomeClass someClass in list)
{
if(someClass.In tProperty == 345)
{
return true;
}
}
return false;
}
}
Chris