views:

139

answers:

2
public class Parser
{
    Downloader download = new Downloader();
    HtmlDocument Page;

    public Parser(string MovieTitle)
    {
        Page = download.FindMovie(MovieTitle);
    }

    public Parser(string ActorName)
    {
        Page = download.FindActor(ActorName);
    }
}

I want to create a constructor that will allow other developers who use this library to easily create a Parser object with the relevant HtmlDocument already loaded as soon as it's done creating it.

The problem lies in that a constructor cannot exist twice with the same type of parameters. Sure I can tell the logical difference between the two paramters, but the computer can't.

Any suggestions on how to handle this?

Thank you!

+17  A: 

Use a couple static methods instead:

public class Parser
{
    Downloader download = new Downloader();
    HtmlDocument Page;

    private Parser() { } // prevent instantiation from the outside

    public static Parser FromMovieTitle(string MovieTitle)
    {
        var newParser = new Parser();
        newParser.Page = newParser.download.FindMovie(MovieTitle);
        return newParser;
    }

    public static Parser FromActorName(string ActorName)
    {
        var newParser = new Parser();
        newParser.Page = newParser.download.FindActor(ActorName);
        return newParser;
    }
}
Mehrdad Afshari
I was about to correct my errors when I realized you had a correct answer up.
ChaosPandion
Woah woah woah, I'm trying to follow the code but my mind is having seizures. I can't understand how I would then use this class from another class say "TestClass.cs". Can you elaborate a bit please. This is something pretty new to me and it's exciting!
Sergio Tapia
@Sergio, from another class, you would not say `Parser parser = new Parser(actorName);`. You would instead obtain an instance by calling the static method, `Parser parser = Parser.FromActorName(actorName);` With the code Mehrdad provided, the only way to obtain an instance from outside the class itself is through one of the static methods. Those static methods would use the private constructor to create an instance and then return it to the call site.
Anthony Pegram
Wow, this is so amazing. Thank you so much for sharing guys. All the best!
Sergio Tapia
+4  A: 

In this particular case, I would probably use the static methods (I rarely have public constructors myself), but another possibility is to have shim classes, and this technique is also a way you can overload static methods with unique parameter signatures instead of giving them unique (and potentially unwieldy) names:

public class Parser
{
    Downloader download = new Downloader();
    HtmlDocument Page;

    public Parser(MovieTitle MovieTitle)
    {
        Page = download.FindMovie(MovieTitle);
    }

    public Parser(ActorName ActorName)
    {
        Page = download.FindActor(ActorName);
    }
}

Then your usage is new Parser(new MovieTitle(str)) or new Parser(new ActorName(str))

This is a preferable paradigm if you are considering constructing objects from IDs. If you have ints or Guids for your object IDs, there is a possibility of a constructor needing to take two different "types" of Guids. The alternative, of constructing from full objects, is far safer and eliminates a lot of silly mistakes when you pass an int. It just makes sense to not expose internal implementation features as much as possible between objects.

Cade Roux