473,230 Members | 1,880 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,230 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 1736
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
7
by: farseer | last post by:
Here is the scenario: I have an interface which defines get methods for data that will make up a row in a table. However, the source of this data may, over time, switch/change (The company may...
8
by: Charles Law | last post by:
I am implementing the command pattern in VB.NET, where the commands have been serialised. That is, I have several classes that all inherit from my base Command class, that implements ICommand...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
5
by: Diffident | last post by:
Hello All, I am designing a class based on singleton pattern. Inside this class I have multiple instance methods. My question is since there will be only one instance of this class at any...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
4
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.