473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Duplicate constructor code issue

I have encountered this problem a few times and would like to see how
other people deal with it.

A library has a Constructor that takes in n params and does some basic
validation and populates the class member.
I then get a requirement to add another paramter in at initialisation
time so the options I have (as far as I can see) are:

1. Change the current interface with the new param(s). No duplicate code
but the existing interface is changed and can cause me backwards
compatibilty problems.

2. Create a new constructor and give the user the choice. Problem is the
code in Constructor one must be copied in here as the same
initialisation is required.

3. I don't allow the user to pass in the new params. I simply make a
public member (or a get/set) and hope the user picks this up.

4. Create a base constructor common to all constructors.
This involves another class but might be the best solution.
The base constructor has all the standard validation I need, and the
child classes have the specific params validation etc.

Interested to hear other peoples views on this.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Apr 8 '07 #1
3 1678
use the "this" construct to invoke the other constructor, as in

public Foo(p1, p2, p3) //original constructor
{
//do assignments/logic with p1, p2, p3
}
public Foo (p1, p2, p3, p4):this(p1, p2, p3) //new constructor, with
p4, invokes
//prev
construct. via "this"
{
//assign/logic with p4
}
"Steven Blair" <st**********@btinternet.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I have encountered this problem a few times and would like to see how
other people deal with it.

A library has a Constructor that takes in n params and does some basic
validation and populates the class member.
I then get a requirement to add another paramter in at initialisation
time so the options I have (as far as I can see) are:

1. Change the current interface with the new param(s). No duplicate code
but the existing interface is changed and can cause me backwards
compatibilty problems.

2. Create a new constructor and give the user the choice. Problem is the
code in Constructor one must be copied in here as the same
initialisation is required.

3. I don't allow the user to pass in the new params. I simply make a
public member (or a get/set) and hope the user picks this up.

4. Create a base constructor common to all constructors.
This involves another class but might be the best solution.
The base constructor has all the standard validation I need, and the
child classes have the specific params validation etc.

Interested to hear other peoples views on this.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***

Apr 8 '07 #2
Fred,

Exactly what I am looking for.

Thanks :)

*** Sent via Developersdex http://www.developersdex.com ***
Apr 8 '07 #3
"Fred Mellender" <no****************@frontiernet.netwrote in message
news:m8*****************@news02.roc.ny...
use the "this" construct to invoke the other constructor, as in

public Foo(p1, p2, p3) //original constructor
{
//do assignments/logic with p1, p2, p3
}
public Foo (p1, p2, p3, p4):this(p1, p2, p3) //new constructor,
with p4, invokes

//prev construct. via "this"
{
//assign/logic with p4
}
It is generally a better Idea to have the constructor with less
parameters call the one with more parameters, passing in some default
value for the additional parameter

Change this
public Foo(p1, p2, p3) //original constructor
{
//do assignments/logic with p1, p2, p3
}

to this
public Foo(p1, p2, p3) : this (p1, p2, p3, 0) //new 3 param constructor
{ }

public Foo(p1, p2, p3, p4) //new 4 param constructor
{
//do assignments/logic with p1, p2, p3, p4
}

The library code continues to call the 3 param constructor, but it is
now a passthrough to the 4 param constructor.

This allows you to put the initialization in ONE constructor and simply
have the others call that constructor with the appropriate defaults.

Bill


Apr 8 '07 #4

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

Similar topics

0
by: Andrew Chanter | last post by:
I have been working with an A97 database that performs a data processing function. It imports data from a flat text file then uses a dao transaction that executes a number of sql statements (about...
10
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this...
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) {
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: Pete Davis | last post by:
I know I can use reflection to call internal, protected, and private methods in a class, but I need to instantiate an object that is public but has an internal constructor (The CurrencyManager...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
6
by: Dave | last post by:
I really don't like the users getting an unhandled expception page, and I'm still to new with ASP.Net and C#. So please accept my appology for the 2 part question. SqlException (0x80131904) ...
11
by: Are Nybakk | last post by:
Hi, I've stumbled into a problem I just can't figure out. I found out that variables in JS don't have local scope inside brackets in say, a loop, but things still doesn't add up. Also, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
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,...

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.