473,385 Members | 1,693 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,385 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 2013
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, ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.