473,465 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constructor overloads

What do you do in a situation where you have more than 1 overload with
the same signature (e.g. class(string, string, string, string))? Is the
best think to do to send through an extra parameter such as an int as an
identifier?
Regards,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
5 1114
Hi Mike,

I would prefer an enumeration over int to distinguish the various
constructors.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Having 2 constructors with the same signature would never compile. You would
have to vary number/type of parameters.

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
What do you do in a situation where you have more than 1 overload with
the same signature (e.g. class(string, string, string, string))? Is the
best think to do to send through an extra parameter such as an int as an
identifier?
Regards,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Mike,

You can't do that. The compiler will complain when you try and do this
for a constructor, or any method. You will have to add an extra flag
indicating what the other parameters represent.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
What do you do in a situation where you have more than 1 overload with
the same signature (e.g. class(string, string, string, string))? Is the
best think to do to send through an extra parameter such as an int as an
identifier?
Regards,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
Mike P <mr*@telcoelectronics.co.uk> wrote:
What do you do in a situation where you have more than 1 overload with
the same signature (e.g. class(string, string, string, string))? Is the
best think to do to send through an extra parameter such as an int as an
identifier?


One alternative is to use factory methods which differ in name:

MyType (string name, string country)
{
this.name = name;
this.country = country;
}

public static MyType FromName (string name)
{
return new MyType (name, null);
}

public static MyType FromCountry (string country)
{
return new MyType (null, name);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5


From a code-readability standpoint, you're probably better off going with
fewer constructors, then setting properties after you've created an instance
of the object. Trying to pass everything into multiple constructors can get
messy.

-James
"Mike P" wrote:
What do you do in a situation where you have more than 1 overload with
the same signature (e.g. class(string, string, string, string))? Is the
best think to do to send through an extra parameter such as an int as an
identifier?
Regards,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6

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

Similar topics

19
by: Daniel Billingsley | last post by:
I think it's really nice to be able to do code like someVariable = someMethod(new SomeClass("some text")); However, as method signatures are number and types of parameters, you're limited to...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
1
by: shapper | last post by:
Hello, I am creating a class but somehow I think I am not getting the constructors right. I get the error: "Public Overloads Sub Add has multiple definitions to with identical signatures" ...
2
by: Ethan Strauss | last post by:
Hi, I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.