Constructor chaining

G

Guest

Hello,

I have this situation where I have two overloaded constructors and I would
like to call one constructor from another without using the initializer way.
I.e. without doing:

public myClass(/*param list*/) : this(/* more param list */)

internal myClass(/*more param list */)

(Firstly if the terminology that I am using is wrong then I apologise and I
would appreciate it if you could tell the correct terminology.)

The reason why I don't want to call one constructor from another like this
is because I have to perform some statements before the call to the
overloaded constructor. So I would like to do something like this:

public myClass(/*param list*/)
{
// do some stuff
// ...
this(/*more param list*/);
}

internal myClass(/*more param list*)
{
// initialise the class
}

Is this possible without using methods:
public myClass(/*param list*/)
{
// do some stuff
Init(...);
}

internal myClass(/*more param list*)
{
Init(...);
}

protected Init(...)
{
// initialise the class
}

I think it would cleaner to only use constructors and not use methods but is
it possible?

Thank you
Sidharth
 
T

the.duckman

I'm not too sure if init methods can't modify readonly feilds. THis
might cause someproblems.

I struggle to think of a circumstance where a calss would need to
perform actions before its parent.

but If faced with the problem I would do this...


class animal
{
public animal()
{
init();
}

protected virtual void init()
{
}
}



class dog : animal
{
public dog() :base()
{
}

protected override void init()
{
//code to run first on dog goes here
base.init()
}
}



If I didn't stuff up my thought pattern calling "new dog()" results in
the following calls.
animal()
dog.init()
animal.init()
dog()

Hence you can have the chiken come before the egg.

-dm
 
J

Jon Skeet [C# MVP]

Sidharth said:
I have this situation where I have two overloaded constructors and I would
like to call one constructor from another without using the initializer way.

You can't do this, I'm afraid. Your Init method is the best way of
proceeding if you really have to do other things first. (I'd make it
private though.)

Jon
 
G

Guest

Hi DM,

Thanks for the reply. It's a very clever solution but I feel it may not be
viable in my situation. I should've provided more information in the first
place, sorry. What is happening is the constructor is doing 2 things:
creating the details in the database and then initiating the object itself.
So originally I had only one constructor for the class:

class MyClass
{
public MyClass(string Name)
{
using (SqlConnection conn ....)
{
conn.Open();
SqlTransaction trans ...
// calls to insert stored procudures
// initialise this object.
}
}
}

This class is actually in the middle tier of an application. Everything was
going nicely when due to some change I found that I actually had to call this
constructor from another class in the middle tier - which does some other
insertions into the database before calling this object. So we have this
scenario:

class MySecondClass
{
// ...

public Method(string Name, string otherInformation)
{
using (SqlConnection conn ...)
{
conn.Open();
SqlTransaction ....
// insert some stuff into the database
new MyClass(Name, /* conn and trans to go here */);
}
}
}

As you can see from my other class I want to pass the connection and
transaction to the first class. To do this I would have to change the
signature of the first class to allow conn and transaction objects. However I
do not want the front end to know anything about conn and transactions so I
thought to change the first class in this manner:

class MyClass
{
internal MyClass(string Name, SqlConnection conn, SqlTransaction trans)
{
// calls to insert stored procudures
// initialise this object.
}

public MyClass(string Name)
{
using (SqlConnection conn ....)
{
conn.Open();
SqlTransaction trans ...
this(Name, conn, trans);
}
}
}

By making the constructor with the conn and trans as internal the front end
doesn't know anything about the conn and trans and everything is fine in the
world again. But unfortunately I can't call this(); from inside the
constructor.

Hmm, actually maybe your solution would still work in this scenario but now
I'm thinking that maybe the way I have designed this from the start is wrong.
Should a constructor be used to do what it is doing just now. Or should I
separate out the creation of the database details as a factory class?
Something like this:

class MyClass
{
public MyClass(int Id)
{
// initialise this object.
}
}

class MyClassCreation
{
public static CreateMyClass(string Name)
{
using (SqlConnection conn ....)
{
conn.Open();
SqlTransaction trans ...
// calls to insert stored procudures
// initialise this object.
return new MyClass(Id);
}
}
}

Is this correct/better way of doing it?

Thank you for your help

Sidharth
 
G

Guest

Hi Jon,

Thank you for your advice. In your opinion do you think it would be better
to do it as a factory class? Please see my reply to DuckMan's post for more
information.

Thank you once again
Sidharth
 
J

Jon Skeet [C# MVP]

Sidharth said:
Thank you for your advice. In your opinion do you think it would be better
to do it as a factory class? Please see my reply to DuckMan's post for more
information.

I think it would depend on the responsibilities of the rest of the
class - does it do other database activity? I'm afraid it's hard to see
without a bigger picture (which would no doubt take a fair amount of
explaining).

Of course, you could put the static (factory) method within the same
class...

Jon
 
J

Joanna Carter [TeamB]

"Sidharth" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| Thanks for the reply. It's a very clever solution but I feel it may not be
| viable in my situation. I should've provided more information in the first
| place, sorry. What is happening is the constructor is doing 2 things:
| creating the details in the database and then initiating the object
itself.
| So originally I had only one constructor for the class:

Pardon me for saying so, but this really is not a good idea. You are tying
your business objects to a database that could change, thus forcing you to
have to change all your business classes whenever that kind of thing
happens.

Take a look at separating your business classes from their storage using
something known as an OPF (Object Persistence Framework). These creatures
use reflection to examine objects and store/retrieve/delete them from a
database without the objects knowing anything at all about databases. They
really are a better way to go.

Joanna
 
G

Guest

Hi Jon,

Yeah the class does perform other database activities - when an update
happens to the object.

I've changed it to use a static Create method inside the same class and it
looks nice!

Thank you for your help again.

Sidharth
 
G

Guest

Hi Joanna,

Thank you for your reply. I totally agree with you. The way it is done is
not ideal - unfortunately this is a 4 year old project with a lot of code and
to move everything would take a lot of effort. Despite saying that, thank you
for the information and I will definitely check OPF out and will separate out
the business and data access components for the new changes.

Thank you once again

Sidharth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top