Constructor Chaining

E

escher4096

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()
{

}
 
E

escher4096

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
 
E

escher4096

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
 
R

raylopez99

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
 
J

Jon Skeet [C# MVP]

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
 
G

G.S.

<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.
 
J

Jon Skeet [C# MVP]

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
 
G

G.S.

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
 

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