<p>I have a Person object with two constructors - one takes an int (personId), the other a string (logonName).  I would like another constructor that takes a string (badgeNumber).  I know this can't be done, but seems it might be a common situation.  Is there a graceful way of handling this?  I suppose this would apply to any overloaded method.  Code:</p>

<pre><code>public class Person
{
    public Person() {}

    public Person(int personId)
    {
        this.Load(personId);
    }

    public Person(string logonName)
    {
        this.Load(logonName);
    }

    public Person(string badgeNumber)
    {
        //load logic here...
    }
</code></pre>

<p>...etc.</p>

[{'answer_id': 31220, 'author': 'Zack Peterson', 'author_id': 83, 'author_profile': 'https://Stackoverflow.com/users/83', 'pm_score': 4, 'selected': True, 'text': '<p>You might consider using custom types.</p>\n\n<p>For example, create LogonName and BadgeNumber classes.</p>\n\n<p>Then your function declarations look like...</p>\n\n<pre><code>public Person(LogonName ln)\n{\n    this.Load(ln.ToString());\n}\n\npublic Person(BadgeNumber bn)\n{\n    //load logic here...\n}\n</code></pre>\n\n<p>Such a solution might give you a good place to keep the business logic that governs the format and usage of these strings.</p>\n'}, {'answer_id': 31222, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 1, 'selected': False, 'text': '<p>No.</p>\n\n<p>You might consider a flag field (enum for readability) and then have the constructor use htat to determine what you meant.</p>\n'}, {'answer_id': 31224, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 0, 'selected': False, 'text': "<p>Only thing I can think of to handle what you're wanting to do is to have to params, one that describes the param type (an enum with LogonName, BadgeNumer, etc) and the second is the param value. </p>\n"}, {'answer_id': 31228, 'author': 'Landon', 'author_id': 1597, 'author_profile': 'https://Stackoverflow.com/users/1597', 'pm_score': 1, 'selected': False, 'text': "<p>That won't work.  You might consider making a class called BadgeNumber that wraps a string in order to avoid this ambiguity.</p>\n"}, {'answer_id': 31229, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 4, 'selected': False, 'text': '<p>You could perhaps use factory methods instead?</p>\n\n<pre><code>public static Person fromId(int id) {\n    Person p = new Person();\n    p.Load(id);\n    return p;\n}\npublic static Person fromLogonName(string logonName) {\n    Person p = new Person();\n    p.Load(logonName);\n    return p;\n}\npublic static Person fromBadgeNumber(string badgeNumber) {\n    Person p = new Person();\n    // load logic\n    return p;\n}\nprivate Person() {}\n</code></pre>\n'}, {'answer_id': 31230, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 1, 'selected': False, 'text': '<p>You cannot have two different constructors/methods with the same signature, otherwise, how can the compiler determine which method to run.</p>\n\n<p>As <a href="https://stackoverflow.com/questions/31215/constructors-with-the-same-argument-type#31220">Zack said</a>, I would consider creating an "options" class where you could actually pass the parameters contained in a custom type. This means you can pretty much pass as many parameters as you like, and do what you like with the options, just be careful you dont create a monolithic method that tries to do everything..</p>\n\n<p>Either that, or vote for the <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow noreferrer">factory pattern</a>..</p>\n'}, {'answer_id': 31232, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 1, 'selected': False, 'text': '<p>You could use a static factory method:</p>\n\n<pre><code>public static Person fromLogon(String logon) { return new Person(logon, null); }\npublic static Person fromBadge(String badge) { return new Person(null, badge); }\n</code></pre>\n'}, {'answer_id': 31233, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 0, 'selected': False, 'text': "<p>You could switch to a factory style pattern.</p>\n\n<pre><code>public class Person {\n\n  private Person() {}\n\n  public static PersonFromID(int personId)\n  {\n    Person p = new Person().\n    person.Load(personID);\n\n    return p;\n    this.Load(personId);\n  }\n\n  public static PersonFromID(string name)\n  {\n    Person p = new Person().\n    person.LoadFromName(name);\n\n    return p;\n  }\n\n  ...\n}\n</code></pre>\n\n<p>Or, as suggested, use custom types. You can also hack something using generics, but I wouldn't recommend it for readability.</p>\n"}, {'answer_id': 31239, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 1, 'selected': False, 'text': '<p>As has been suggested, custom types is the way to go in this case.</p>\n'}, {'answer_id': 31240, 'author': 'Drakiula', 'author_id': 2437, 'author_profile': 'https://Stackoverflow.com/users/2437', 'pm_score': -1, 'selected': False, 'text': '<p>How about ...</p>\n\n<pre><code>public Person(int personId)\n{\n    this.Load(personId);\n}\n\npublic Person(string logonName)\n{\n    this.Load(logonName);\n}\n\npublic Person(Object badgeNumber)\n{\n    //load logic here...\n}\n</code></pre>\n'}, {'answer_id': 31299, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 2, 'selected': False, 'text': "<p>You have four options that I can think of, three of which have already been named by others:</p>\n\n<ol>\n<li><p>Go the factory route, as suggested by several others here.  One disadvantage to this is that you can't have consistent naming via overloading (or else you'd have the same problem), so it's superficially less clean.  Another, larger, disadvantage is that it precludes the possibility of allocating directly on the stack.  Everything will be allocated on the heap if you take this approach.</p></li>\n<li><p>Custom object wrappers.  This is a good approach, and the one I would recommend if you are starting from scratch.  If you have a lot of code using, e.g., badges as strings, then rewriting code may make this a non-viable option.</p></li>\n<li><p>Add an enumeration to the method, specifying how to treat the string.  This works, but requires that you rewrite all the existing calls to include the new enumeration (though you can provide a default if desired to avoid some of this).</p></li>\n<li><p>Add a dummy parameter that is unused to distinguish between the two overloads.  e.g. Tack a <code>bool</code> onto the method.  This approach is taken by the standard library in a few places, e.g. <code>std::nothrow</code> is a dummy parameter for <code>operator new</code>.  The disadvantages of this approach are that it's ugly and that it doesn't scale.</p></li>\n</ol>\n\n<p>If you already have a large base of existing code, I'd recommend either adding the enumeration (possibly with a default value) or adding the dummy parameter.  Neither is beautiful, but both are fairly simple to retrofit.</p>\n\n<p>If you are starting from scratch, or only have a small amount of code, I'd recommend the custom object wrappers.</p>\n\n<p>The factory methods would be an option if you have code which heavily uses the raw <code>badge</code>/<code>logonName</code> strings, but doesn't heavily use the <code>Person</code> class.</p>\n"}, {'answer_id': 32153, 'author': 'Jake Pearson', 'author_id': 632, 'author_profile': 'https://Stackoverflow.com/users/632', 'pm_score': 1, 'selected': False, 'text': '<p>If you are using C# 3.0, you can use <a href="http://msdn.microsoft.com/en-us/library/bb384062.aspx" rel="nofollow noreferrer">Object Initializers</a>:</p>\n\n<pre><code>public Person()\n{\n}\n\npublic string Logon { get; set; }\npublic string Badge { get; set; }\n</code></pre>\n\n<p>You would call the constructor like this:</p>\n\n<pre><code>var p1 = new Person { Logon = "Steve" };\nvar p2 = new Person { Badge = "123" };\n</code></pre>\n'}, {'answer_id': 66372459, 'author': 'Salim', 'author_id': 2800651, 'author_profile': 'https://Stackoverflow.com/users/2800651', 'pm_score': 0, 'selected': False, 'text': '<p>Depending on your business constraints:</p>\n<pre><code>public class Person\n{\n    public string Logon { get; set; } = &quot;&quot;;\n    public string Badge { get; set; } = &quot;&quot;;\n    \n    public Person(string logon=&quot;&quot;, string badge=&quot;&quot;) {}\n}\n// Use as follow \nPerson p1 = new Person(logon:&quot;MylogonName&quot;);\nPerson p2 = new Person(badge:&quot;MyBadge&quot;);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31215', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1865/']