472,973 Members | 2,416 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Constructor Chaining

I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

Everything that class B calls in its constructor has been marked
internal or private so I can't just decompile it and copy and paste
its constructor code out without copying several other functions (all
of which also call private and internal methods - you can see this
would get out of control).

In C# you chain by doing:

public MyClassName() :[this or base](arguments)
{
}

what I ned is something more like:

public MyClassName()
{

}
Sep 11 '08 #1
8 2001
On Sep 11, 2:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

Everything that class B calls in its constructor has been marked
internal or private so I can't just decompile it and copy and paste
its constructor code out without copying several other functions (all
of which also call private and internal methods - you can see this
would get out of control).

In C# you chain by doing:

public MyClassName() :[this or base](arguments)
{

}

what I ned is something more like:

public MyClassName()
{

}
oops miscued and posted by accident :)

what I need is something more like:

public MyClassName()
{
try
{
[this or base](arguments);
}
catch(blah blah blah)
{
throw new meaningful exception....
}
}

Is it possible to do this in C#?

Thanks

-Cam
Sep 11 '08 #2
On Sep 11, 2:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

Everything that class B calls in its constructor has been marked
internal or private so I can't just decompile it and copy and paste
its constructor code out without copying several other functions (all
of which also call private and internal methods - you can see this
would get out of control).

In C# you chain by doing:

public MyClassName() :[this or base](arguments)
{

}

what I ned is something more like:

public MyClassName()
{

}
oops miscued and posted too soon :)

what I need is something more like:

public MyClassName()
{
try
{
base(arguments);
}
catch(blah blah blah)
{
thgrow new meaningful exception
}
}

Is this possible in C#?

Thanks

-Cam

Sep 11 '08 #3
On Thu, 11 Sep 2008 13:59:03 -0700, escher4096 <ca*****@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.
You can't do it.

If the constructor throws an exception, then the object isn't properly
constructed and thus is unusable. I never thought about it before, but in
hindsight this may be why C# puts the call to a base constructor outside
the method body, rather than inside the way Java does it, to make it very
clear that you can't legitimately catch exceptions thrown by base
constructors.

If your class would be usable even though the base class isn't properly
constructed, then your own class probably shouldn't be inheriting that
class anyway. You may be better off compositing the class into your own,
and providing whatever fallback implementation is appropriate when you
can't create an instance of class B.

Pete
Sep 11 '08 #4
On Sep 11, 2:10*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>
If your class would be usable even though the base class isn't properly *
constructed, then your own class probably shouldn't be inheriting that *
class anyway. *You may be better off compositing the class into your own, *
and providing whatever fallback implementation is appropriate when you *
can't create an instance of class B.
Yes, I agree with Pete here. I say make the misbehaving base class
"virtual" so that it doesn't do much, and let the derived class do the
real work.

RL

Sep 11 '08 #5
On Sep 11, 9:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.
<snip>

As others have said, you can't do this. What you *can* do is make your
constructor private and expose static methods to create instances.
They can do error handling however you want.

Jon
Sep 12 '08 #6
On Sep 12, 2:02*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Sep 11, 9:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

<snip>

As others have said, you can't do this. What you *can* do is make your
constructor private and expose static methods to create instances.
They can do error handling however you want.

Jon
Or wrap A's construction in try/catch and handle, because as Peter
said, you don't want the A object whose base B construction failed
anyway.
Sep 12 '08 #7
On Sep 12, 4:35*pm, "G.S." <gstoy...@gmail.comwrote:
Or wrap A's construction in try/catch and handle, because as Peter
said, you don't want the A object whose base B construction failed
anyway.
But if the purpose is to rewrap the exception in a more meaningful
one, you don't want to have to do that at *every* call site. By
wrapping it within a single static method, class A is correctly taking
responsibility for things.

Jon
Sep 12 '08 #8
On Sep 12, 11:48*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Sep 12, 4:35*pm, "G.S." <gstoy...@gmail.comwrote:
Or wrap A's construction in try/catch and handle, because as Peter
said, you don't want the A object whose base B construction failed
anyway.

But if the purpose is to rewrap the exception in a more meaningful
one, you don't want to have to do that at *every* call site. By
wrapping it within a single static method, class A is correctly taking
responsibility for things.

Jon
yes, yes, you're right, of course but I guess I was thinking that if
its rethrown, it's gotta be handled
Sep 12 '08 #9

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

Similar topics

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...
19
by: Rob | last post by:
I've just started writing managed C++ (VS 2005 Beta 2) and I really have not read a whole lot about the differences between managed and unmanaged C++. Using the class wizard, I can create a...
7
by: Geoffrey | last post by:
Hello, I wan't to have optionnal parameter in my cronstructor, so, I define 2 constructors. when I call the constructor without the "optional" parameter, I want to call the constructor...
3
by: needin4mation | last post by:
The code is taken from the book Professional C#: abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; }...
2
by: DaTurk | last post by:
Hi, I was wondering if there is a CLI equivalent to using the this keyword to overload constructors. You know where you would do something like MyClass() : this("something") { }
20
by: danishce | last post by:
I want to encrypt the ASCII message using Cipher Block Chaining MAC and generate the 8 byte key.How can i achieve this? Thanks: Danish Majid
1
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.