ESD
ESD

Reputation: 545

How to use the public struct of an object in a class

So, I have this class and 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;
    }
    ...

What I want is to be able to create a variable of the type of this struct in the class where I have the object containing this struct :

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

How can I achieve this?

Upvotes: 0

Views: 114

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

If you want to initialize a voiture you have to access it via the outer class, since the full-name is CarCollection.voiture:

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

You're using an instance of the outer class to reference the struct.

Nested Types (C# Programming Guide)

Upvotes: 2

Related Questions