473,385 Members | 1,912 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,385 software developers and data experts.

Finding objects in an array

Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
Person myPerson = people.GetThePersonWithName("Fred");

// Do things with "Fred"
...
}
----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jun 27 '08 #1
7 2522
Array.Find:

Person fred = Array.Find<Person>(people, delegate(Person
person)
{
return person.Name == "Fred";
});

you might be able to drop the <Personthere... or even easier in C# 3
(still against .NET 2.0):

Person fred = Array.Find(people, person =person.Name ==
"Fred");

then:

if(fred != null)
{ // Do things with "Fred"
}

Marc
Jun 27 '08 #2
In C# 3.5:

People[] freds = Array.FindAll(people, p =p.FirstName == "Fred");
if(freds != null && freds.Length != 0)
{
// do things with freds
}

In C# 2.0:

People[] freds = Array.FindAll(people, delegate(Person p)
{
return p.FirstName == "Fred";
});
if(freds != null && freds.Length != 0)
{
// do things with freds
}

You could perform a contains check with Array.IndexOf first, but that
would involve 2 scans through the array.

Terry.

On Jun 16, 12:23 pm, Dylan Parry <use...@dylanparry.comwrote:
Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
Person myPerson = people.GetThePersonWithName("Fred");

// Do things with "Fred"
...}

----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parryhttp://electricfreedom.org|http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.


Jun 27 '08 #3
Dylan Parry wrote:
Is there a simple way to do the above?
[...]

Thanks to all that replied. As I'm able to use 3.5, I've gone with the
LINQ solution. I did eventually come up with something similar myself,
but it was much more complicated and not at all elegant!

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jun 27 '08 #4
On Jun 16, 4:23*pm, Dylan Parry <use...@dylanparry.comwrote:
Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
* * Person myPerson = people.GetThePersonWithName("Fred");

* * // Do things with "Fred"
* * ...}

----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parryhttp://electricfreedom.org|http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.

C# 3.5

class Program
{
static void Main(string[] args)
{
List<Personpersons = GetPersons();

foreach (Person per in persons.Where(p =p.Name ==
"Fred"))
{
Console.WriteLine(per.Name);
}
Console.ReadLine();
}

public static List<PersonGetPersons()
{
List<Personpersons = new List<Person>();
persons.Add(new Person("Alex"));
persons.Add(new Person("Robert"));
persons.Add(new Person("Fred"));
persons.Add(new Person("Freak"));
persons.Add(new Person("Google"));
persons.Add(new Person("App"));

return persons;
}
}

This should work!!!
Jun 27 '08 #5
On Jun 16, 4:23*pm, Dylan Parry <use...@dylanparry.comwrote:
Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
* * Person myPerson = people.GetThePersonWithName("Fred");

* * // Do things with "Fred"
* * ...}

----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parryhttp://electricfreedom.org|http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
C# 2.0

List<Personpersons = GetPersons();

Predicate<Personpred = delegate(Person p)
{
return p.Name == "Fred";
};

List<Personmatches = persons.FindAll(pred);

-Cnu
Jun 27 '08 #6
C# 3.5

Pedant mode; there is no C# 3.5; that is C# 3 (from the lambda)
using .NET 3.5 - or actually it could be .NET 2.0 with LINQBridge or
any other similar LINQ-to-objects implementation. Either way, the
beauty of LINQ on IEnumerable<Tis that it will work even with the
OP's original array design (you don't need the List<T>).

Marc
Jun 27 '08 #7
On Jun 16, 5:09*pm, Marc Gravell <marc.grav...@gmail.comwrote:
C# 3.5

Pedant mode; there is no C# 3.5; that is C# 3 (from the lambda)
using .NET 3.5 - or actually it could be .NET 2.0 with LINQBridge or
any other similar LINQ-to-objects implementation. Either way, the
beauty of LINQ on IEnumerable<Tis that it will work even with the
OP's original array design (you don't need the List<T>).

Marc
Thanks for the info. I think you are right.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
2
by: John Spiegel | last post by:
Hi all, I'm trying to create a subclass of an ArrayList that is intended specifically for holding FileInfo objects. My idea to restrict this is to override the Add and AddRange methods to allow...
1
by: emma middlebrook | last post by:
Hi I want to find out what objects are due to receive an event i.e. those that have added themselves as an event handler via +=. Yes, it's a little pointless perhaps (or can anyone give some...
2
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to...
2
by: equinox1248 | last post by:
Hi, I thought this would be answered several time in this group, but I couldn't find anything relevant. What would be the most efficient way of calculating sum of absolute values of two...
4
by: muralibala68 | last post by:
Hi, Is there a way to find the total size of the DOMDocument created using xerces C++ API (without serializing it) ? Thanks.
25
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like...
15
by: timothytoe | last post by:
Situation: I have an array of objects. I want to find the maximum value of a given numeric value that exists in each of the objects. Suppose the array of objects is called "stat" and the numeric...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.