Behseini
Behseini

Reputation: 6320

How to pass a List of a Class A in Constructor of Class B

I am able to pass a an instance of an object (Director class) in the constructor of the Movie() class like

public Movie( string title, Director directorName ){
     Title = title;
     director=directorName;

    }

This is totally understandable while there is a One to One relationship between Movie() and Director() like each movie only has one director but what if a movie has two director? (one to many)? How can I pass a list of directors in the constructor of movie? and how I can get them in Console.WriteLine(m1.director.Name)?

void Main()
{
    Director d1 = new Director("Wachowski Brothers", "USA"); 
    Movie m1 = new Movie("Matrix", d1);
    Console.WriteLine(m1.Title);
    Console.WriteLine(m1.director.Name);
}

class Movie
{
    public Director director; 
    public string Title { get; set; }

    public Movie( string title, Director directorName ){
     Title = title;
     director=directorName;

    }
}
class Director
{
    public string Name { get; set; }
    public string Nationality { get; set; }

    public Director(string name, string nationality){
        Name = name;
        Nationality = nationality;
    }
}

Update

I tried this in Movie() constructor

public Movie( string title, List<Director> directors ){
 Title = title;
 director= directors;
}

but I am getting this error

cannot implicitly convert type 'system.collections.generic.list<UserQuery.Director>' to 'UserQuery.Director'

on director= directors.

Upvotes: 0

Views: 75

Answers (1)

csebal
csebal

Reputation: 56

C# is a strongly typed language. Meaning that you cannot freely substitute types inside a class for one another.

This means, that for an entity like a movie, where you have potentially multiple directors, you need to use a collection type and work around that in other ways.

Two things to answer here:

a) In order to get two (or more) directors for a movie, you need them stored in a collection of some sort. I suggest a generic List for this purpose.

class Movie
{
    public List<Director> Directors { get; set; }

    public Movie(List<Director> directors) 
    { 
        Directors = directors; 
    }
}

(things to read up on here: Generics in C# - how a single list can contain various things)

b) In order to be able to get the names of the directors in one field, you will want to introduce a property in your movie. After all, A director only has one name, but a movie might have several directors.

class Movie
{
    public string DirectorNames
    {
        get
        {
            return Directors.Aggregate("", (result, director) => string.Concat(result, result.Length>0?", ":"", director.Name);
        }
    }
}

(things to read up on here: lambda functions - what => means in the code)

The code above will output the name or names of the directors in the list as a string, with a comma separating each director, if there is more than one.

After that, you would use Console.WriteLine(m1.DirectorNames) instead of Console.WriteLine(m1.director.Name);

Upvotes: 1

Related Questions