Some construction that I don't understand why it works as it does.

T

Tony Johansson

Hello!

I have this small class CustomException with one ctor see below.
In this class I have both override the ToString and Message.

Now to my question if I made this call
string ss = ToString(); in the ctor for this class CustomException
I call the overridden ToString in this class CustomException.

If I made this call string sss = base.ToString();
in the ctor for this class CustomException I also call the overridden
ToString in this class CustomException

Now can somebody explain why second example will call the ToString in my
class CustomException
If I write little pseudocode I can understand it if we had this
construction.
I just use base as an instance variable of CustomException
Exception base = new CustomException()
base.ToString();
because here the dynamic type is CustomException and we will have polymorf
call so ToString in class CustomException will be called.

But when we have string sss = base.ToString();
what is the dynamic type for base?


public class CustomException : Exception
{
private MathClass mathobject;
private string sMessage;

public CustomException(string sMsg, MathClass mo)
{
mathobject = mo;
sMessage = sMsg;

//string ss = ToString();
//string sss = base.ToString();
}

override public string Message
{
get{return String.Format("Message is <{0}>, Object is {1}",
sMessage, mathobject.ToString());}
}

override public string ToString()
{
string s = Message;
s += "\nException thrown from ";
s += TargetSite;
return s;
}
}

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
Hello!

I have this small class CustomException with one ctor see below.
In this class I have both override the ToString and Message.

Now to my question if I made this call
string ss = ToString(); in the ctor for this class CustomException
I call the overridden ToString in this class CustomException.

If I made this call string sss = base.ToString();
in the ctor for this class CustomException I also call the overridden
ToString in this class CustomException

Now can somebody explain why second example will call the ToString in my
class CustomException
If I write little pseudocode I can understand it if we had this
construction.
I just use base as an instance variable of CustomException
Exception base = new CustomException()
base.ToString();
because here the dynamic type is CustomException and we will have polymorf
call so ToString in class CustomException will be called.

But when we have string sss = base.ToString();
what is the dynamic type for base?

When you use base.Whatever() you aren't making a virtual call. The
method implementation in the base class is *always* used. So, that will
call Exception.ToString().

Note that calling virtual methods in a constructor is generally a bad
idea for precisely this kind of reason.

Personally I would favour formatting the message in the constructor and
passing it up to the base constructor, without overriding anything.
That avoids many of the pitfalls of inheritance. See

http://msmvps.com/blogs/jon.skeet/archive/2006/03/04/inheritancetax.asp
x

for more on that topic.
 

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