Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

named constructor idiom in c#

  • 25-08-2008 1:22am
    #1
    Closed Accounts Posts: 5,284 ✭✭✭


    is there an equivalent for this in c#?

    ie: a way of differentiating between different constructors for the same class with the same parameter sets

    I'm using passing the parameters in different orders / passing dummy parameters for the moment until I find a neater way of doing it.


Comments

  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    Overload the constructor as required.. or have you the same type of paramaters for each constructor you need?


  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    Yeah exactly that - the same same type of parameters

    eg:

    public constructor(int a, string b){...}
    public constructor(int c, string d){...}


  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    Function signature will be the same so unless you use a common constructor with enums or a different name its not possible


  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    wonder why they didn't make this possible in c#.

    You can do it in c++ as described here:
    http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8


  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    Ahh ok

    try

    public functionname:this

    Its the this bit that allows you to do it


  • Advertisement
  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    I don't follow what you are saying
    I'm not trying to reuse code between the two constructors.


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    Could you re-arrange your code so that you only need to use one constructor?

    Something like:

    [PHP]
    public constructor(int a, string b)
    {
    ...
    if (b == "Something")
    {
    // Do stuff
    }
    else if (b == "Something Else")
    {
    // Do other stuff
    }
    ...
    }
    [/PHP]


  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    Not in this case.
    The context is this:
    I have a class with three constructors:

    myclass() //used for a new instance not loaded from db
    myclass(Guid tbl1_id) //used when passed Guid for a db table (loads data from db)
    myclass(Guid tbl2_id) //used when passed Guid from different db table (loads data from db)

    At the moment I work around it by using a dummy parameter to differentiate the latter two classes, but I thought there should be a more elegant method.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Can you not just pass in an extra parameter that identifies the table you are loading the data from?


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    pwd wrote: »
    Not in this case.
    The context is this:
    I have a class with three constructors:

    myclass() //used for a new instance not loaded from db
    myclass(Guid tbl1_id) //used when passed Guid for a db table (loads data from db)
    myclass(Guid tbl2_id) //used when passed Guid from different db table (loads data from db)

    At the moment I work around it by using a dummy parameter to differentiate the latter two classes, but I thought there should be a more elegant method.
    Okay, how about defining the constructor so that it always expects a GUID. Something like this:

    [PHP]
    public constructor(int a, Guid b)
    {
    ...
    if (b == null)
    {
    // Do stuff
    }
    else if (b == "Table_1_GUID")
    {
    // Do Table 1 stuff
    }
    else if (b == "Table_2_GUID")
    {
    // Do Table 2 stuff
    }
    ...
    }
    [/PHP]


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Do you really need to do that load in the constructor, couldn't you just have one constructor and then LoadFromTableA and LoadFromTableB methods?


  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    the constructors are very different
    the guids are the primary keys of records in db tables so the if statement outlined above wouldn't work.
    the different methods and a parameter indicating which one would work.


  • Registered Users, Registered Users 2 Posts: 15,065 ✭✭✭✭Malice


    pwd wrote: »
    the constructors are very different
    the guids are the primary keys of records in db tables so the if statement outlined above wouldn't work.
    I don't see how the if statements wouldn't work, especially if you used the LoadFromTableA(), LoadFromTableB() methods as Stevenmu suggested. Of course, I'm not looking at your code so I could be mis-understanding the scenario :).
    pwd wrote:
    the different methods and a parameter indicating which one would work.
    I don't understand what you're trying to say here.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    malice_ wrote: »
    I don't understand what you're trying to say here.

    I think he means what I suggested:

    myclass(tablename, GUID)


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    I would say just pass in the tablename or an enum specifying the table.


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Define classes which extend the original class, and override the constructor?

    so as well as a MyClass, you'd have a Guid1MyClass and a Guid2MyClass. Assign them to variables of type MyClass, and Robert is the brother of your father.

    No?

    ETA: Not coming from a C++ background, I find the idea of named constructors inelegant. Subclassing seems (to me) to be a more OO way of dealing with the problem


  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    Sorry forgot about this.. subclassing is the best option in C# for this


Advertisement