Home All Groups Group Topic Archive Search About

Passing in different int context on same signature?

Author
11 Mar 2006 6:57 PM
Brett Romero
I'd like to pass two different int contexts on the same constructor
signature.  For example:

Class1(int PersonId)
{...}

Class1(int EmployeeId)
{...}

Of course the above does not work because of the signatures.  So I do a
goofy work around:

Class1(int EmployeeId, int forEmployeeId)
{...}

Where forEmployeeId gets any int value and is never processing.  It's
only there to distinguish the two constructors I need.

I always have more Persons than Employees.  A Person may not be an
Employee but an Employee is always a Person.  Is there a better way to
do the above?  My work around works fine but isn't very good. I know
there is a more correct way of doing this and that's what I'd like to
get at.  I'd like to avoid switches and conditionals.  I'm using .NET
2.0 so please pull out all of the stops if need be. 

Thanks,
Brett

Author
11 Mar 2006 7:40 PM
Otis Mukinfus
Show quote Hide quote
On 11 Mar 2006 10:57:21 -0800, "Brett Romero" <acco***@cygen.com> wrote:

>I'd like to pass two different int contexts on the same constructor
>signature.  For example:
>
>Class1(int PersonId)
>{...}
>
>Class1(int EmployeeId)
>{...}
>
>Of course the above does not work because of the signatures.  So I do a
>goofy work around:
>
>Class1(int EmployeeId, int forEmployeeId)
>{...}
>
>Where forEmployeeId gets any int value and is never processing.  It's
>only there to distinguish the two constructors I need.
>
>I always have more Persons than Employees.  A Person may not be an
>Employee but an Employee is always a Person.  Is there a better way to
>do the above?  My work around works fine but isn't very good. I know
>there is a more correct way of doing this and that's what I'd like to
>get at.  I'd like to avoid switches and conditionals.  I'm using .NET
>2.0 so please pull out all of the stops if need be. 
>
>Thanks,
>Brett
If an employee is always a person then instead of using an employeeid, use a
PersonID and a bool property to indicate whether or not a person is an employee.
Then you can do this with the constructor:

Person(21, true); // person 21 is an employee
and
have an additional ctor:
Person(21); // Default to not an employee, or is an employee (whichever meets
your needs best).

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Are all your drivers up to date? click for free checkup

Author
11 Mar 2006 9:37 PM
Brett Romero
I don't think it is any different than how I'm doing it now.  Just that
you are using a bool where I'm using an int.  Neither of the 2nd
parameters will be processed.  They are just providing different
signatures.

Brett
Author
11 Mar 2006 10:36 PM
Otis Mukinfus
On 11 Mar 2006 13:37:52 -0800, "Brett Romero" <acco***@cygen.com> wrote:

>I don't think it is any different than how I'm doing it now.  Just that
>you are using a bool where I'm using an int.  Neither of the 2nd
>parameters will be processed.  They are just providing different
>signatures.
>
>Brett
If you are not processing the second parameter in your two parameter example how
will you tell upon retrieval if one of the objects you are creating is an
employee or not?

My vision of the solution was to have one ID and a method for determining after
the fact that the object was or was not an employee.

I'm confused by your logic there, unless you have another way to determine
employee status upon retrieval.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Author
11 Mar 2006 11:10 PM
Brett Romero
Otis,  you know it is a person if this constructor is used:

Class1(int personid) {...}

You know it is an employee if this constructor is used:

Class1(int employeeid, int justaflagforemployee){...}

Brett
Author
11 Mar 2006 11:39 PM
Otis Mukinfus
On 11 Mar 2006 15:10:12 -0800, "Brett Romero" <acco***@cygen.com> wrote:

>Otis,  you know it is a person if this constructor is used:
>
>Class1(int personid) {...}
>
>You know it is an employee if this constructor is used:
>
>Class1(int employeeid, int justaflagforemployee){...}
>
>Brett
Yes, I understand that part.

Now that you have constructed the object and put it in a list or something and
you go back to get it again, how will you know which constructor was used?

Your second example is the same as using a bool.  So you *are* processing the
second parameter.  That's not what you said earlier.


Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Author
12 Mar 2006 12:31 AM
Brett Romero
No, I'm not processing the second parameter.  Here's what I do:  When
the constructor with one parameter is used, I initialize everything
regularly (have faith here).  When the two parameter constructor is
used, there is additional logic that will get me Person related
information, including the PersonId, which I already have when the
single parameter constructor is used.

So, the two parameter constructor does everything the one parameter
does and additional gathering of information (person information).  By
the time that is done, I'm in the same state as if some one had used
the single parameter constructor, which is the ultimate state I want to
be in.

Brett
Author
11 Mar 2006 10:22 PM
Michael Nemtsev
Hello Otis,

I'd use interfaces for this.
class takes base interface,  IEmployee, and in class body cast this interface
to the requires type or check with "is" keyword

OM> On 11 Mar 2006 10:57:21 -0800, "Brett Romero" <acco***@cygen.com>
OM> wrote:
OM>
Show quoteHide quote
>> I'd like to pass two different int contexts on the same constructor
>> signature.  For example:
>>
>> Class1(int PersonId)
>> {...}
>> Class1(int EmployeeId)
>> {...}
>> Of course the above does not work because of the signatures.  So I do
>> a goofy work around:
>>
>> Class1(int EmployeeId, int forEmployeeId)
>> {...}
>> Where forEmployeeId gets any int value and is never processing.  It's
>> only there to distinguish the two constructors I need.
>>
>> I always have more Persons than Employees.  A Person may not be an
>> Employee but an Employee is always a Person.  Is there a better way
>> to do the above?  My work around works fine but isn't very good. I
>> know there is a more correct way of doing this and that's what I'd
>> like to get at.  I'd like to avoid switches and conditionals.  I'm
>> using .NET 2.0 so please pull out all of the stops if need be.
>>
>> Thanks,
>> Brett
OM> If an employee is always a person then instead of using an
OM> employeeid, use a PersonID and a bool property to indicate whether
OM> or not a person is an employee. Then you can do this with the
OM> constructor:
OM>
OM> Person(21, true); // person 21 is an employee
OM> and
OM> have an additional ctor:
OM> Person(21); // Default to not an employee, or is an employee
OM> (whichever meets
OM> your needs best).
OM> Otis Mukinfus
OM> http://www.arltex.com
OM> http://www.tomchilders.com
---
WBR,
Michael  Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Author
11 Mar 2006 10:43 PM
Otis Mukinfus
On Sat, 11 Mar 2006 22:22:26 +0000 (UTC), Michael Nemtsev <nemt***@msn.com>
wrote:

>Hello Otis,
>
>I'd use interfaces for this.
>class takes base interface,  IEmployee, and in class body cast this interface
>to the requires type or check with "is" keyword
>

That is a more elegant way to do it Michael, and if a database were involved you
could make both objects appear the same to the DB.  I like it.  However, upon
retrieval from the DB you would still need a way to determine which type of
object a row contained.

It's like storing peoples names in a table.  If you must know their sex, you
have to have a way to tell if they are male or female.  Typically that would be
with a "sex" column.
[snip]

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Bookmark and Share