2011-06-27 64 views
0

我目前在學習關於結構的知識,所以我有以下練習: 設置一個名爲「Date」的Struct,其中包含date,包括年,月和日。另外,定義一個名爲Phone的類,其中包含姓名,號碼,出生日期和地址。您需要創建一個包含Phone類型對象的數組,並按名稱,數字和日期對它們進行排序。 好了,所以這是代碼:結構練習上的問題

struct Date 
{ 
    int year, month, day; 
    public Date(int year, int month, int day) 
    { 
     this.year = year; 
     this.month = month; 
     this.day = day; 
    } 
    public int Year 
    { 
     get { return year; } 
     set {year = value; } 
    } 
    public int Month 
    { 
     get { return month; } 
     set { month = value; } 
    } 
    public int Day 
    { 
     get { return day; } 
     set { day = value; } 
    } 
} 
    class Phone 
{ 
    string number; 
    string adress; 
    string name; 
    Date birthday = new Date(); 
    public Phone(string number,Date birthday, string adress, string name) 
    { 
     this.number = number; 
     this.birthday = birthday; 
     this.adress = adress; 
     this.name = name; 
    } 

} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Phone[] p = new Phone[3]; 
     p[0] = new Phone(1072548, 
    } 
} 

我沒有錯誤,但問題是,我不知道如何從「日期」結構得到的生日,這就是爲什麼我不再往在信息中。 謝謝。

+0

爲什麼要用Class創建日期?並使用結構? – hashi

+2

你爲什麼要模仿有生日特性的手機?這是電話的生日嗎?多麼奇怪的事情要跟蹤。 – asawyer

+0

大聲笑,我不明白你們,你是說我做錯了什麼或者問題只是愚蠢? –

回答

2
p[0] = new Phone(1072548, 
    new Date (1999, 12, 31), 
    "Central Park, NY", "Sam Party") 

也許這添加到您的日期結構:

public DateTime ToDateTime() 
{ 
    return new DateTime (Year, Month, Day); 
} 

然後,你可以這樣排序的數組:

array.OrderBy (p => p.BirthDate.ToDateTime()); 
+0

謝謝!另一件事,我是使用二分搜索對數組進行排序還是..? –

+0

@很好,看看我對這個問題的回答。這可能會有所幫助,但是您可能需要我提到的網頁中描述的非通用選項。 http://stackoverflow.com/questions/6488201/binary-search-a-list-of-custom-data-types-to-match-just-one-field/6488234#6488234 –

+0

我還沒有學到它,不過謝謝! –

0
struct Date 
{ 
    Date() 
    { 
     //code 
    } 
} 

class Phone 
{ 
    Phone(string "someParametr") 
    { 
     //code 
    } 
} 

需要兩個承包商。