2014-08-29 52 views
-2

我正在爲我的兼職編程課程寫作業。我的代碼的問題是array.find()和搜索的結果。它應該(在我的理論中)在數組中搜索信息,然後將它們發佈給用戶,但是所有搜索結果都是一樣的:ass2task1.Program + customer這裏只有代碼的一部分,因爲老師告訴我們,我們可以在互聯網上,只要張貼問題,因爲我們當您轉換對象爲字符串("The forename resuts:" + resultforename)不要張貼我們的整個代碼Array.find()提供了奇怪的結果

struct customer 
    { 
     public int customernumber; 
     public string customersurname; 
     public string customerforname; 
     public string customerstreet; 
     public string customertown; 
     public DateTime customerdob; 
    } 

    static void Main(string[] args) 
    { 
     customer[] customerdetails = new customer[99]; 
     int selector = 0; 
     int selector2 = 0; 
     string vtemp = ""; 
     string ctemp = ""; 
     int searchnumber; 
     string searchforename; //variable/ array declaring 
     string searchsurname; 
     string searchtown; 
     DateTime searchdob; 
     customer resultnumber; 
     customer resultforename; 
     customer resultsurname; 
     customer resulttown; 
     customer resultdob; 


    if (selector2 == 2) 
        { 
         Console.Clear(); 
         Console.WriteLine("Enter the forename you are looking for: "); 
         searchforename = (Console.ReadLine()); 
         resultforename = Array.Find(customerdetails, customer => customer.customerforname == searchforename); 
         Console.Clear(); 
         Console.WriteLine("Enter the surname you are looking for: "); // all of the searches comes out with ass2task1.Program+customer result 
         searchsurname = (Console.ReadLine()); 
         resultsurname = Array.Find(customerdetails, customer => customer.customersurname == searchsurname); 
         Console.WriteLine("The forename resuts:" + resultforename); 
         Console.WriteLine("The surname resuts:" + resultsurname); 
+0

實際上你是否正在向任何客戶填充客戶詳細信息數組?代碼只顯示你聲明一個數組,它將容納99個項目 – 2014-08-29 14:50:17

+0

在你的註釋行「ass2task1.Program + customer」什麼是ass2task1?另外,你將selecttor2設置爲0,然後有一個if塊,只有當selector2 var是2時纔會被命中,你能提供更相關的代碼來解決你的問題嗎?另外你是用數據填充你的數組嗎? – Kritner 2014-08-29 14:54:23

+0

@Kritner'ass2task1'可能是命名空間。 – Ric 2014-08-29 14:55:47

回答

1

Array.Find()將返回匹配的斷言,如果你想要的屬性值,你需要做的是這樣的對象:resultforename.customerforname或類似的東西。

如果沒有找到它,那麼默認值將被退回,所以檢查空值等

+0

謝謝,添加到代碼,它現在完美。 – Skretek112 2014-08-29 16:11:16

+0

很高興我能幫到你。 – Ric 2014-08-29 21:25:22

1

它調用對象的方法ToString()。定義一個適當的ToString()方法:

struct customer 
{ 
    public int customernumber; 
    public string customersurname; 
    public string customerforname; 
    public string customerstreet; 
    public string customertown; 
    public DateTime customerdob; 

    public override string ToString() 
    { 
     return customersurname + ", " + customerforname; 
    } 
} 
0

要在裏克和clcto的回答(嘗試)擴展。在

Console.WriteLine("The forename resuts:" + resultforename); 
Console.WriteLine("The surname resuts:" + resultsurname); 

你resultforename你得到的結構名稱的原因是客戶結構的 - 默認Console.WriteLine(結構)不知道如何表達一個複雜的對象爲字符串。

至於建議你可以做

Console.WriteLine("The forename resuts:" + resultforename.customerforname); 

或爲結構提供自己的ToString()方法clcto指出的 - 這樣做基本上就Console.WriteLine(或任何字符串表示)如何將客戶的結構表示爲字符串。

不知道這是否會有所幫助,或使其更加清晰。但給定:

public struct Foo 
{ 
    public string Bar { get; set; } 
} 

public struct FooTwo 
{ 
    public string Bar { get; set; } 

    public override string ToString() 
    { 
     return "This is how to represent a Foo2 as string: " + Bar; 
    } 
} 

Foo[] foos = new Foo[99]; 

Foo foundFoo = foos[0]; // This is equivalent to your find statement... setting a foundFoo local variable to a Foo struct 
string foundBar = foos[0].Bar; // This is another way to get to what you're trying to accoomplish, setting a string value representation of your found object. 

Console.WriteLine(foundFoo); // Doesn't know how to deal with printing out a Foo struct - simply writes [namespace].Foo 
Console.WriteLine(foundFoo.Bar); // Outputs Foo.Bar value 
Console.WriteLine(foundBar); // Outputs Foo.Bar value 

FooTwo foo2 = new FooTwo(); 
foo2.Bar = "test bar"; 

Console.WriteLine(foo2); // outputs: "This is how to represent a Foo2 as string: test bar"