0

I am having issue on passing/ connecting one class Director() in the constructor of another class Movie(). Basically what I want to do is to have two different domain for Movie and Director but use instance of Director in constructor of the Movie class. I have a declaration of public Director director; in the movie class but how I can pass it as constructor parameter in Movie?

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

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

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

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

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

5 Answers 5

4

You don't want to pass the Name, but the whole instance. So instead of

Movie m1 = new Movie("Matrix", d1.Name);

you mean

Movie m1 = new Movie("Matrix", d1);
4
  • Thanks Rene but how I can access the value of the d1? I mean in case of getting the nationality of director or even setting it ? How I can do it in main?
    – Mona Coder
    Sep 22, 2017 at 17:30
  • I don't really understand that question? You do have d1 in scope in Main, so you can either d1.Nationality = ... or via the movie m1.director.Nationality = ....
    – René Vogt
    Sep 22, 2017 at 17:32
  • Ok when I try Console.WriteLine(m1.director.Nationality); I am getting this error NullReferenceException: Object reference not set to an instance of an object.
    – Mona Coder
    Sep 22, 2017 at 17:37
  • @MonaCoder Oh yes, because in your Movie constructor you did not set the director field to the passed directorName parameter.
    – René Vogt
    Sep 22, 2017 at 17:43
2
Movie m1 = new Movie("Matrix", d1.Name);

should be

Movie m1 = new Movie("Matrix", d1);
1

try this :

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;
    }
}
1
  • While this answers the question, you should consider adding some explanatory text that describes what was wrong with the OP's code. This would be beneficial to other users. Sep 22, 2017 at 18:13
0

You can create director in same time movie like this:

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

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

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

    }

    public Movie( string title, string DirectorName, string DirectorNationality){
     Title = title;
     director=new Director(DirectorName, DirectorNationality);

    }
}


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

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

Like the above posts suggest. Your function signature.

public Movie( string title, Director directorName )

calls for an object of type Director. You are passing in only the name, which is a String. Naming is important here. You are calling Director "directorName". That's where you're getting confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.