所以我創建了一個包含字符串,整數和浮點數的類。c#爲特定的int值搜索對象數組,然後返回該對象的所有數據
然後我在主那些類型的聲明的陣列和在該類型的對象讀入它
現在我需要搜索該陣列爲一特定值,並且如果該值匹配,則返回整個對象
我該怎麼做呢?
真的難倒
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)
只是FYI,如果你被限制在一個不能使用LINQ的.NET版本中,那麼你很可能不會使用lambda表達式語法來判斷'Array.Find()'...'Object found = Array的謂詞。 Find(myArray,delegate(MyType o){return o.Foo == someValue;});' – 2011-03-21 22:47:58
@Quintin點和非常真實 – BrokenGlass 2011-03-21 23:52:02