PC Review


Reply
Thread Tools Rate Thread

All Values Same (for property) in a Collection

 
 
sloan
Guest
Posts: n/a
 
      1st May 2007

What is the most concise way to ...... bail out of a loop (or return false)
if one value isn't the same as all the others in a collection?


For example:


I have an EmpCollection, which is a collection of Employee objects.
Employee object has a .DeptID property (int)

Let's say I want to verify all DeptID 's are the same for a collection of (N
number) of employees.


public bool AllDeptIDsMatch ( EmpCollection ec )
{
bool returnValue = false; // or true

for each (Employee e in ec)
{

Console.WriteLine ( e.DeptID ) ;
//???/

}



return returnValue;

}


For clarity,

e1.DeptID = 101;
e2.DeptID = 101;
e3.DeptID = 101;
the above would return true
...........
e1.DeptID = 101;
e2.DeptID = 101;
e3.DeptID = 101;
e4.DeptID = 202;
e5.DeptID = 101;
e6.DeptID = 101;
the above would return false.




I've written some code , but seems too verbose.

This is still for 1.1 fyi.




 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      1st May 2007
sloan,

Just return, or if you want to go to the code after the loop, use break:

public bool AllDeptIDsMatch(EmpCollection ec)
{
// This assumes you have a count property. If there are
// no items, or one item, they are all the same.
if (ec.Count <= 1)
{
// They are all the same.
return true;
}

// Get the first value.
int id = ec[0].DeptID;

// Cycle through all the employees. If the department id
// is different for any of them, return false.
foreach (Employee e in ec)
{
if (e.DeptID != id)
{
// They are not all the same.
return false;
}
}

// They are the same.
return true;
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"sloan" <(E-Mail Removed)> wrote in message
news:ubvt$(E-Mail Removed)...
>
> What is the most concise way to ...... bail out of a loop (or return
> false)
> if one value isn't the same as all the others in a collection?
>
>
> For example:
>
>
> I have an EmpCollection, which is a collection of Employee objects.
> Employee object has a .DeptID property (int)
>
> Let's say I want to verify all DeptID 's are the same for a collection of
> (N
> number) of employees.
>
>
> public bool AllDeptIDsMatch ( EmpCollection ec )
> {
> bool returnValue = false; // or true
>
> for each (Employee e in ec)
> {
>
> Console.WriteLine ( e.DeptID ) ;
> //???/
>
> }
>
>
>
> return returnValue;
>
> }
>
>
> For clarity,
>
> e1.DeptID = 101;
> e2.DeptID = 101;
> e3.DeptID = 101;
> the above would return true
> ..........
> e1.DeptID = 101;
> e2.DeptID = 101;
> e3.DeptID = 101;
> e4.DeptID = 202;
> e5.DeptID = 101;
> e6.DeptID = 101;
> the above would return false.
>
>
>
>
> I've written some code , but seems too verbose.
>
> This is still for 1.1 fyi.
>
>
>
>



 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      1st May 2007

Thanks Nicholas ....

I was missing the ec.Count thing.

It's been a long 2 months at work. I need a vacation.

...

Sloan



"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> sloan,
>
> Just return, or if you want to go to the code after the loop, use

break:
>
> public bool AllDeptIDsMatch(EmpCollection ec)
> {
> // This assumes you have a count property. If there are
> // no items, or one item, they are all the same.
> if (ec.Count <= 1)
> {
> // They are all the same.
> return true;
> }
>
> // Get the first value.
> int id = ec[0].DeptID;
>
> // Cycle through all the employees. If the department id
> // is different for any of them, return false.
> foreach (Employee e in ec)
> {
> if (e.DeptID != id)
> {
> // They are not all the same.
> return false;
> }
> }
>
> // They are the same.
> return true;
> }
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "sloan" <(E-Mail Removed)> wrote in message
> news:ubvt$(E-Mail Removed)...
> >
> > What is the most concise way to ...... bail out of a loop (or return
> > false)
> > if one value isn't the same as all the others in a collection?
> >
> >
> > For example:
> >
> >
> > I have an EmpCollection, which is a collection of Employee objects.
> > Employee object has a .DeptID property (int)
> >
> > Let's say I want to verify all DeptID 's are the same for a collection

of
> > (N
> > number) of employees.
> >
> >
> > public bool AllDeptIDsMatch ( EmpCollection ec )
> > {
> > bool returnValue = false; // or true
> >
> > for each (Employee e in ec)
> > {
> >
> > Console.WriteLine ( e.DeptID ) ;
> > //???/
> >
> > }
> >
> >
> >
> > return returnValue;
> >
> > }
> >
> >
> > For clarity,
> >
> > e1.DeptID = 101;
> > e2.DeptID = 101;
> > e3.DeptID = 101;
> > the above would return true
> > ..........
> > e1.DeptID = 101;
> > e2.DeptID = 101;
> > e3.DeptID = 101;
> > e4.DeptID = 202;
> > e5.DeptID = 101;
> > e6.DeptID = 101;
> > the above would return false.
> >
> >
> >
> >
> > I've written some code , but seems too verbose.
> >
> > This is still for 1.1 fyi.
> >
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding to property of property of object collection TS Microsoft ASP .NET 3 31st Aug 2006 01:57 PM
Fill collection property from a property grid Vlado Microsoft Dot NET Framework Forms 2 29th Mar 2006 08:22 AM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
Edit collection property from Property window... nobody Microsoft ASP .NET 7 17th Mar 2004 11:23 PM
Using Enumerated Values to Access Property Collection Gos Microsoft VB .NET 1 11th Sep 2003 04:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:16 AM.