2011-06-27 30 views
0

これは練習です: 年、月、日を含む日付を含む "Date"という構造体を設定します。また、名前、番号、生年月日、住所を含む "Phone"というクラスを定義します。 Phone型のオブジェクトを含む配列を作成し、名前、番号、日付で並べ替える必要があります。 これは私のコードです:だからstructの基本的なエクササイズ

struct Date 
{ 
    int year, month, day; 
    public Date(int year, int month, int day) 
    { 
     this.year = year; 
     this.month = month; 
     this.day = day; 
    } 
} 
class Phone 
{ 
    int number; 
    string birthday, adress, name; 
    public Phone(int number, string 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]; 
     for (int i = 0; i < p.Length; i++) 
     { 
     } 
    } 
} 

、事は、私が「電話」クラスから構造体の日付を取得する方法がわからないということです。 これはこのようなものと思われますか?誕生日、年など。おかげさまで

+0

誕生日を「生年月日」といいます。あなたはDate構造体を作成しました...今、それを使用します。 –

答えて

1

birthdaystringと宣言しています。

Dateと宣言する必要があります。

1

現在、birthdayは文字列として表示されていますが、実際にはDateにしたいと思っています。フィールドとコンストラクタパラメータの両方をDateにします。

また、すべての値に対して「ゲッター」プロパティがほとんど存在するはずです。そうしないと、structインスタンスを作成した後にデータを取得できなくなります。

0

ここで何をしようとしているのか分かりません。あなたは、 "日付"のインスタンスではなく、文字列として "誕生日"を渡しています。

お誕生日は、あなたがこれを行うために必要があると思い日にしたい場合:

public class Phone 
{ 
    public int Number {get; set;} 
    public string Name {get; set;} 
    public Date Birthday {get; set;} 
    public string Address {get; set;} 

    public Phone(int number, Date birthday, string name, string address) 
    { /* your implementation here */ } 
} 

お誕生日のためにあなたの電話のコンストラクタに文字列を渡したい場合は、日付の何かが必要だろうそれを変換する構造体:

public Phone(int, number, string birthday, string name, string address) 
{ 
    Number = number; 
    Birthday = Date.FromString(birthday); 
    Name = name; 
    Address = address; 
} 

そしてそのDate.FromString(string date)はStructのメソッドになります。

+0

申し訳ありませんが、あなたが私に言ったことを理解しました、今これはコードです: –

0

誕生日の変数を日付として宣言する必要があります。あなたは構造体を必要としません。