Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# search an array of objects for a specific int value, then return all the data of that object

Tags:

arrays

c#

So i've created a class that holds strings, ints, and floats.

then i declared an array in main of those types and read in objects of that type into it

now i need to search that array for a specific value, and if that value matches, then return the whole object

how would i go about doing this?

really stumped

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)
like image 970
John Avatar asked Mar 21 '11 22:03

John


4 Answers

You could use a simple LINQ extension method to search for the object.

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

Here is some MSDN information regarding LINQ to get you more familiar.

EDIT for the code posted.

I first want to say it looks like you are bringing across some paradigms from a different language, like java with your getter method instead of using .NET style properties, something you might want to look into. But I have made a code example more tailored to your specific case..

You can replace the block

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

With

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

Or using the Array.Find() method as suggested by BrokenGlass..

cdClass foundCD = Array.Find(cdArray, delegate(cdClass cd) { return cd.get_upc() == UPC); });
like image 150
Quintin Robinson Avatar answered Nov 11 '22 15:11

Quintin Robinson


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

var fooItem = Array.Find(myArray, item => item.fooProperty == "bar");
like image 20
BrokenGlass Avatar answered Nov 11 '22 15:11

BrokenGlass


Use LINQ like:

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 you started with LINQ.

like image 1
ebb Avatar answered Nov 11 '22 14:11

ebb


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 you'll only have one match where T1 == 4, if you're going to have more than one then do something like this:

var instances = from thing in list
                where thing.T1 == 4
                select thing;
like image 1
kprobst Avatar answered Nov 11 '22 15:11

kprobst