473,503 Members | 3,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

One Constructor Calling Another

Greetings, all:

I want to invoke one constructor from another to gain code reuse.

In the snip below, I want my second constructor to pass its first argument
to the first constructor so that the first constructor can set the Provider
property and invoke the resetConnection() method first, before the second
constructor assigns the connection string to the Connection property, but I
can't seem to work out the syntax.

public class DataProcessor {

// all unrelated code omitted.

public DataProcessor(DataProvider provider) {
Provider = provider;
resetConnection();
}

public DataProcessor(DataProvider provider, string connectionString) {
// this.DataProcessor(provider); <-- How do I do this?
Connection.ConnectionString = connectionString;
}
}

--
Peace & happy computing,

Mike Labosh, MCSD
"I have no choice but to believe in free will."
Nov 16 '05 #1
4 2222
Without the comment you are actually pretty close. What you do is call
it like:

public class DataProcessor {

// all unrelated code omitted.

public DataProcessor(DataProvider provider) {
Provider = provider;
resetConnection();
}

public DataProcessor(DataProvider provider, string
connectionString) : this(provider) {
Connection.ConnectionString = connectionString;
}
}

Have A Better One!

John M Deal, MCP
Necessity Software

Mike Labosh wrote:
Greetings, all:

I want to invoke one constructor from another to gain code reuse.

In the snip below, I want my second constructor to pass its first argument
to the first constructor so that the first constructor can set the Provider
property and invoke the resetConnection() method first, before the second
constructor assigns the connection string to the Connection property, but I
can't seem to work out the syntax.

public class DataProcessor {

// all unrelated code omitted.

public DataProcessor(DataProvider provider) {
Provider = provider;
resetConnection();
}

public DataProcessor(DataProvider provider, string connectionString) {
// this.DataProcessor(provider); <-- How do I do this?
Connection.ConnectionString = connectionString;
}
}

Nov 16 '05 #2

Hi Mike,

Constructors have their 'own special way' of calling or utilising each
other, from the same class,

public class DataProcessor {

// all unrelated code omitted. OK...
public DataProcessor(DataProvider provider)
{
Provider = provider;
resetConnection();
}

public DataProcessor(DataProvider provider, string connectionString) :
this(provider)//<--Do this
{
// this.DataProcessor(provider); <-- No you don't do this?
Connection.ConnectionString = connectionString;
}
}

When doing so keep in mind how overloading works and 'this(...)' will work
for the appropriate constructor. HTH.

Regards,
SpotNet.
"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:OK**************@TK2MSFTNGP15.phx.gbl...
Greetings, all:

I want to invoke one constructor from another to gain code reuse.

In the snip below, I want my second constructor to pass its first argument
to the first constructor so that the first constructor can set the
Provider property and invoke the resetConnection() method first, before
the second constructor assigns the connection string to the Connection
property, but I can't seem to work out the syntax.

public class DataProcessor {

// all unrelated code omitted.

public DataProcessor(DataProvider provider) {
Provider = provider;
resetConnection();
}

public DataProcessor(DataProvider provider, string connectionString) {
// this.DataProcessor(provider); <-- How do I do this?
Connection.ConnectionString = connectionString;
}
}

--
Peace & happy computing,

Mike Labosh, MCSD
"I have no choice but to believe in free will."

Nov 16 '05 #3
Mike Labosh <ml*****@hotmail.com> wrote:
I want to invoke one constructor from another to gain code reuse.


See http://www.pobox.com/~skeet/csharp/constructors.html

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

--
Peace & happy computing,

Mike Labosh, MCSD
"I have no choice but to believe in free will."
Nov 16 '05 #5

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

Similar topics

3
4530
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
6
1881
by: Asfand Yar Qazi | last post by:
Hi, Consider this: class C { int j; public: C(int i) : j(i)
26
21278
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) {
9
2356
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
6
2441
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
5
2101
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
10
2010
by: imutate | last post by:
Some questions about ctors and class members Is v private in the following ? If it is why put the declaration at the top ? Is there any difference to putting it in the private section ? The...
74
15868
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
23719
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
7192
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
7064
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...
1
6974
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
7445
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...
1
4991
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
4665
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
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
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 ...
0
369
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...

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.