473,516 Members | 3,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing in different int context on same signature?

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

Mar 11 '06 #1
8 1189
On 11 Mar 2006 10:57:21 -0800, "Brett Romero" <ac*****@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
Mar 11 '06 #2
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

Mar 11 '06 #3
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" <ac*****@cygen.com>
OM> wrote:
OM>
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
Mar 11 '06 #4
On 11 Mar 2006 13:37:52 -0800, "Brett Romero" <ac*****@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
Mar 11 '06 #5
On Sat, 11 Mar 2006 22:22:26 +0000 (UTC), Michael Nemtsev <ne*****@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
Mar 11 '06 #6
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

Mar 11 '06 #7
On 11 Mar 2006 15:10:12 -0800, "Brett Romero" <ac*****@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
Mar 11 '06 #8
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

Mar 12 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14911
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10052
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
1
2095
by: george doubleu | last post by:
hi, i'm using "gcc version 3.3.3 (Debian)" to compile the program pasted below. I get the two warnings you can see in the remarks. The second warning is perfectly OK for me, but the first one I don't understand. Isn't the "const int *" construct in the signature of func1 a hint for the user, that func1 doesn't modify **arg? Why then is it...
8
2106
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed...
7
2598
by: Wade Wegner | last post by:
Hello, I have been desperately trying to programmatically authenticate a windows user, create their credentials, and then redirect them to a different server while passing the credentials at the same time so that they don't have to login again. Specifically, I have two webservers in the same domain. When I have a user go to Webserver A...
22
25549
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet =...
7
10232
by: Tim | last post by:
When there is a need to pass some dynamic information between 2 managed assemblies, the "Dictionary object" in Generic form can be used as a method parameter to pass the information. The information that needs to be passed can be stored as Key-Value pairs, and the method signature remains the same. That way, handling future requirements of...
9
3330
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should...
10
2094
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. Let's say we have 2 delegates: public delegate void FirstDelegate( int i ); public delegate void SecondDelegate( int i );
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7405
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7547
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.