473,795 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2027
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...@nn owslpianmk.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.co mwrote:
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.co mwrote:
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
6368
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
19
1413
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 managed class defined like: ref class MyClass { public MyClass(void); ~MyClas(void); };
7
5605
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 with complete parameteers list.
3
8300
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
2689
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
6066
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
2703
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 overloaded operator<<, which takes a string as input parameter and returns a reference to an ostream object (std::cout in this case). In the main file I instantiate an object of MyOut and then write a message to console in two different ways: 1....
11
18348
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 any mechanism as super is not supported by c++, atleast by MS vc++ 6.0.
9
3154
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, public class SomeList<T> { public SomeList(SomeList<TthisSomeList)
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
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.