开发者

Problem on a struct exercise

I'm currently learning about structs, so I have the following exercise: Set a Struct that called "Date" that contains date, including: year, month and day. Also, define a class called Phone that contains a name, number, date of birth and address. You need to create开发者_如何学Go an array that contains objects of type Phone and sort them by name, number, and date. Alright, so this is the code:

  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,
     }
}

I'm having no error but the problem is that I don't know how to get the birthday from the "Date" struct, and that's why I stopped putting in the information. Thanks.


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

Maybe add this to your Date struct:

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

Then, you can sort your array like this:

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


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

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

you need two Contractor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜