2012-11-03 55 views
0

所以,我有這個class和struct:如何使用對象的公共結構的一類

class CarCollection 
{ 
    //struct contenant les informations pour décrire une voiture de la collection 
    public struct voiture 
    { 
     public string marque, couleur, condition; 
     public int annee; 
     public DateTime dateAcquisition, dateVente; 
     public double prix; 
    } 
    ... 

我要的是能夠創造這個結構中的類型的變量類我有包含此結構的對象:

class Program 
{ 
    static CarCollection collection; 
    ... 
    voiture temp = new voiture(); 
    //or 
    collection.voiture temp = new collection.voiture(); 

我該如何做到這一點?

+1

試試'new CarCollection.voiture()'。 – adrianbanks

+0

注意:您的結構不代表一個實體,它大於16個字節,並且不可變。考慮改爲使用類。 – Guffa

+0

這是明確指出的assignement所以我沒有選擇... – AntoineLev

回答

2

如果你想初始化一個voiture你必須通過外部類訪問它,因爲全名是CarCollection.voiture

CarCollection.voiture v = new CarCollection.voiture(); 

您使用的是外部類的一個實例來引用結構。

Nested Types (C# Programming Guide)

+0

謝謝你做到了! – AntoineLev

相關問題