C # look for an array of objects for a specific int value and then return all the data of that object

So, I created a class that contains strings, ints and floats.

then I declared an array mainly of these types and read objects of this type in it

now I need to find this array for a specific value, and if this value matches, then return the entire object

how would i do that?

really dead end

public class cdClass { private static string artist = null; private static string genre = null; private static string cdTitle = null; private static float mSRP; private static int stock; private static int upc = 0; //Following functions are public member methods public void read_cd(string artist, string genre, string cdTitle, float mSRP, int stock, int upc) { //cdClass cd = null ; System.Console.WriteLine("Enter Artist Name: "); artist = Console.ReadLine(); System.Console.WriteLine("Enter CD Title: "); cdTitle = Console.ReadLine(); System.Console.WriteLine("Enter Genre Type: "); genre = Console.ReadLine(); System.Console.WriteLine("Enter Manufacturers Suggested Retal Price: "); mSRP = float.Parse(Console.ReadLine()); System.Console.WriteLine("Enter UPC Number: "); upc = int.Parse(Console.ReadLine()); System.Console.WriteLine("Enter Stock: "); stock = int.Parse(Console.ReadLine()); //return cd; } public int get_upc() { return upc; } 

MAIN:

 //Follwoing cod will initialize an array of Cd's cdClass[] cdArray = new cdClass[20]; float taxRate = 0; do { int i = 0; cdClass current_cd = new cdClass(); current_cd.read_cd(artist, genre, cdTitle, mSRP, stock, upc); cdArray[i] = current_cd; i++; } while (businesslogic.question() != 'Y'); buyer = inputfunctions.buyer(); int UPC = inputfunctions.get_upc(); for (int i = 0; i < 20; i++) { if (cdArray[i].get_upc() == UPC) 
+6
arrays c #
source share
5 answers

You can use the simple LINQ extension method to search for an object.

 var foundItem = myArray.SingleOrDefault(item => item.intProperty == someValue); 

Here are some MSDN information regarding LINQ to learn more.

EDIT for the submitted code.

First, I want to say that it looks like you are using some paradigms from another language, for example java with your getter method, instead of using .NET style properties that you might want to learn. But I made a code example that is more appropriate for your specific case.

You can replace the unit

 for (int i = 0; i < 20; i++) { if (cdArray[i].get_upc() == UPC) 

FROM

 cdClass foundCD = cdArray.SingleOrDefault(cd => cd.get_upc() == UPC); 

Or using the Array.Find() method proposed by BrokenGlass ..

 cdClass foundCD = Array.Find(cdArray, delegate(cdClass cd) { return cd.get_upc() == UPC); }); 
+15
source share

Array.Find() is an alternative to LINQ in this special case, especially if you are limited to an older version of .NET:

 var fooItem = Array.Find(myArray, item => item.fooProperty == "bar"); 
+4
source share
 class TheThing { public int T1; public float T2; } List<TheThing> list = new List<TheThing>(); // Populate list... var instance = (from thing in list where thing.T1 == 4 select thing).SingleOrDefault(); 

This assumes that you will only have one match, where T1 == 4 , if you have more than one, then do something like this:

 var instances = from thing in list where thing.T1 == 4 select thing; 
+1
source share

Use LINQ as:

 public class Car { public int ID { get; set; } public int CarName { get; set; } } class Program { public IEnumerable<Car> GetCars { get { return MyDb.Cars; } } static void Main(string[] args) { Car myCar = GetCars.FirstOrDefault(x => x.ID == 5); Console.WriteLine("ID: {0} | CarName {1}", myCar.ID, myCar.CarName); } } 

http://msdn.microsoft.com/en-us/library/bb397926.aspx will provide you with the necessary information to get started with LINQ.

0
source share

For those who are still stuck in .Net 2 ...

The array structure of my class ...

 public class EmployerContactDetailsClass { public string wsStudentID { get; set;} public string wsOrgID { get; set;} public string wsContactID { get; set;} public string wsContactName { get; set;} public string wsContactEmail { get; set;} public string wsEmployerEmail { get; set;} public bool wsUserChoiceVerifier { get; set;} } 

I want to find a specific element in this class based on the passed index value using wsStudentID == 'somevalue' .

First create an array of list

 List<EmployerContactDetailsClass> OrgList = new List<EmployerContactDetailsClass>(); 

Next create one node element

 EmployerContactDetailsClass OrgElem = new EmployerContactDetailsClass(); 

Create your list from some data source (in my case, a Reader object)

  OrgElem.wsOrgID = reader["OrganisationID"].ToString(); //Org ID from database OrgElem.wsContactID = reader["ContactID"].ToString(); //Contact ID from employer database OrgElem.wsUserChoiceVerifier = (bool)reader["UserChoiceVerify"]; //boolean by default if (reader["ContactGivenName"].ToString() != string.Empty && OrgElem.wsUserChoiceVerifier) OrgElem.wsContactName = reader["ContactGivenName"].ToString() + " " + reader["ContactFamilyName"].ToString(); else OrgElem.wsContactName = "TO WHOM IT MAY CONCERN"; OrgElem.wsContactEmail = reader["ContactEmail"].ToString(); //" support@tecnq.com.au "; OrgElem.wsEmployerEmail = reader["OrganisationEmail"].ToString(); //" support@tecnq.com.au "; //now add the elements into the array class OrgList.Add(OrgElem); 

Convert list <> to array []

 EmployerContactDetailsClass[] OrgListArray = (EmployerContactDetailsClass[])OrgList.ToArray(); 

Find the specific StudentID in the array using Delegate .

 String wsStudentID = 'FIND007'; //with respect to Roger Moore RIP EmployerContactDetailsClass OrgElemFound = Array.Find(OrgListArray, ByStudentID(wsStudentID)); 

And the Predicate method is somewhere else on your code ...

 //predicate method to return true or f if studentID matches the array list (used to pass into SendEmail) private Predicate<EmployerContactDetailsClass> ByStudentID(string wsStudentID) { return delegate(EmployerContactDetailsClass OrgElem) { return OrgElem.wsStudentID == wsStudentID; }; } 

Hooray!

0
source share

All Articles