473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1745
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
9195
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
4952
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
2430
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
1827
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
1692
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
3006
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
1595
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
2959
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
2357
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...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
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...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.